nginx源碼安裝
# yum install pcre pcre-devel zlib zlib-devel openssl openssl-devel
# cd /usr/local/src/
# wget http://nginx.org/download/nginx-1.12.2.tar.gz
# tar zxvf nginx-1.12.2.tar.gz
# cd nginx-1.12.2
# ./configure --prefix=/usr/local/nginx
# make && make install
nginx命令
./sbin/nginx 啟動nginx
./sbin/nginx -s reload 重新啟動nginx
./sbin/nginx -s stop 關(guān)閉nginx
./sbin/nginx -t 查看nginx配置文件是否有錯誤
nginx命令添加到環(huán)境變量中
# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
# nginx 直接輸入命令可以使用了
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx配置項
#全局區(qū)
worker_processes 1;//有1個工作的子進程哩至,可以自行修改,但太大無意義蘸炸,因為要爭奪cpu;一般設(shè)置為CPU數(shù)*核數(shù)(4*8), 好的服務(wù)器有4塊cpu,每個cpu是8核级野,所以可以設(shè)置成32
events {
//一般配置nginx連接的特性
// 如一個work能同事連接多少個請求
worker_connections 1024;// 一個worker子進程最大連接1024個連接
}
//主要是用來配置http服務(wù)器
http {
include mime.types;
default_type application/octet-stream;
// log 日志的格式
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
sendfile on;
keepalive_timeout 65;
//配置虛擬主機
server {//最簡單的server,基于域名的虛擬主機
listen 80;
//當訪問test.com ,這個server段就起作用了
server_name test.com;
location / {
root test.com; //這里是相對路徑,相對/usr/local/nginx; 也可以寫絕對路徑
index a.html index.html //默認的首頁, 從左往右
}
}
server {//基于端口的虛擬主機
listen 8080;
server_name test.com;
location / {
root test.com;
}
}
server{//基于ip的虛擬主機
listen 80;
server_name 60.205.231.199,
location / {
root ip;
}
}
nginx 日志管理
...
http{
// log 日志的格式
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
//全局訪問日子的存放的文件,可以被server段里面的配置覆蓋狮辽;使用的格式是'main'格式,main格式可以自定義;main格式是我們定義好的一種日志的格式巢寡,并起個名字喉脖,便于引用;
//$remote_addr 客戶端的ip 122.96.40.215
//$remote_user [$time_local]客戶端的訪問的時間 [30/Mar/2018:11:41:06 +0800]
//$request客戶端的請求 GET / HTTP/1.1
//$status返回的狀態(tài)碼
//$body_bytes_sent返回給客戶端的字節(jié)數(shù)
//$http_referer 客戶端請求當前頁面的來源
//$http_user_agent 用戶的代理瀏覽器
//$http_x_forwarded_for
access_log logs/access.log main
//給某一個server添加訪問日志
server {
listen 80;
server_name test.com;
//該server,它的訪問日志的文件是 logs/access.log;
access_log logs/test.com.log main;
location / {
root test.com;
index index.html
}
}
}
...
nginx定時任務(wù),日志切割
- 把昨天某個server下面的日志改個名稱(用昨天的時間)存起來
# mkdir /data
# vim nginxlog.sh
#!/usr/bin/bash
LOGPATH=/ur/local/nginx/logs/test.com.access.log;
BASEPATH=/data
bakname=$BASEPATH/$(date -d yesterday +"%Y%m%d%H%M").test.com.access.log;
echo $bakname;
mv $LOGPATH $bakname;
touch $LOGPAHT;
kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`;
# chmod +x nginxlog.sh
# crontab -e //添加定時任務(wù)
*/1 * * * * sh /data/nginxlog.sh //每分鐘執(zhí)行一次nginxlog.sh腳本
location相關(guān)
- 定義: 根據(jù)不同的uri抑月,進行不同的路由匹配
- 語法:
location [=|~|~*|^~] match {}
-
匹配類型分三種情況:
- location = match{} 精準匹配
- location match{} 一般匹配
- location ~ match{} 正則匹配
~區(qū)分大小寫树叽;~*不區(qū)分大小寫;^~以摸個字符開頭
-
路由匹配優(yōu)先級:
- 首先看路由中是否有精準匹配爪幻,如果有的話菱皆,停止匹配過程;比如$uri == match匹配成功挨稿,使用configA
location = match{ configA }
- 如果沒有精準匹配與$uri對應(yīng)仇轻;則剩下普通匹配和正則匹配,此時正則匹配優(yōu)先級高于普通匹配奶甘,如果沒有沒有正則匹配與$uri對應(yīng)篷店,最后使用普通匹配;
location / { root /usr/local/nginx/html/image; index index.html; } location ~ image { root /var/www/; index index.html; }
ps: 如果訪問http://www.test.com/image/test.jpg,最終起作用的location是
location ~ image{}
- 如何既沒有精準匹配和正則匹配臭家,只有兩個普通匹配疲陕,如下:
location / { root /usr/local/nginx/html/image; index index.html; } location /test { root html; index index.html; }
ps: 如果訪問http://www.test.com/test,最終起作用的location是location /test {},因為它比較長,所以其作用了
nginx有哪些配置塊
- main(全局配置)
- events(事件配置)
- upstream(上游服務(wù)器設(shè)置)
- http(通用請求處理)
- server(主機設(shè)置)
- location (url匹配特定位置后的設(shè)置)
nginx處理請求的過程
- nginx收到請求后钉赁,根據(jù)請求定位到location,有handler生成response, 再由fileter進行處理蹄殃;nginx模塊開發(fā),可以是Handler模塊開發(fā),也可以是Filter開發(fā)
nginx 重寫
- if 語法格式
if 空格 (條件) {
重寫模式
}
#條件四種書寫方式
#1. = 來判斷相等你踩,用于字符串比較
#2. ~ 用正則來匹配(此正則區(qū)分大小寫)
#3. ~* 不區(qū)分大小寫的正則
#4. -f, -d, -e 來判斷是否為文件诅岩,為目錄,是否存在
例:
//返回狀態(tài)碼带膜,禁止某臺ip的訪問
if ($remote_addr = 127.0.0.1) {
return 403;
}
//重定向到內(nèi)部的某個頁面吩谦,重定向到某個路勁
if ( $http_user_agent ~* chrome) {
rewrite ^.*$ /h5/rewrite.html;//會以root作為根
break;(不break會循環(huán)重定向)
}
root /Users/credan;
nginx常用的系統(tǒng)變量
- $remote_addr //獲取客戶端ip地址
- $http_user_agent //獲取客戶端的信息
nginx配置顯示目錄結(jié)構(gòu)
location ^~ /h5 {
autoindex on; //自動顯示目錄
autoindex_exact_size off; //默認為on,顯示出文件的真實大小膝藕,單位是bytes;改為off后式廷,顯示出文件的大概大小,單位是kb或者MB或者GB;
autoindex_localtime on;//默認為off,顯示的文件時間為GMT時間芭挽;改為on后滑废,顯示的文件為文件的服務(wù)器時間
root /usr/local/www;
}
ps: 該根目錄下面一定不要有index.html文件蝗肪,不然就直接顯示該文件了
nginx配置運行跨域訪問
location ^~ /app {
add_header 'Access-Control-Allow-Origin' '*';
root /usr/local/www;
}
nginx配置反向代理和負載均衡
- 反向代理
server {
listen 80;
server_name test.xxx.com;
root /usr/local/www;
error_log /usr/local/nginx/logs/error.log;
location ^~ /node {
//如果匹配到此路由會反向代理到本地3000端口服務(wù)
proxy_pass http://127.0.0.1:3000;
}
}
- 負載均衡配置
upstream demo {
server 127.0.0.1:3000;
server 127.0.0.1:3001;
}
server {
listen 80;
server_name test.xxx.com;
root /usr/local/www;
error_log /usr/local/nginx/logs/error.log;
location ^~ /node {
proxy_pass http://demo;
}
}