全局設置
#運行用戶
user nginx;
#啓動進程數,通常設置成和cpu的數量相等
worker_processes auto;
#全局錯誤日誌及PID文件
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
#工作模式及連接數上限
events {
use epoll; #epoll是多路複用IO(I/O Multiplexing)中的一種方式,可以大大提高nginx的性能
worker_connections 1024;#單個後臺worker process進程的最大併發鏈接數
}
http服務器設置
設定http服務器,利用它的反向代理功能提供負載均衡支持
http {
#日誌輸出內容與格式
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 /var/log/nginx/access.log main;
#sendfile 指令指定 nginx 是否調用 sendfile 函數(zero copy 方式)來輸出文件,對於普通應用,必須設爲 on,如果用來進行下載等應用磁盤IO重負載應用,可設置爲 off,以平衡磁盤與網絡I/O處理速度,降低系統的uptime.
sendfile on;
tcp_nopush on;
tcp_nodelay on;
#連接超時時間
keepalive_timeout 65;
types_hash_max_size 2048;
#設定mime類型,類型由mime.type文件定義
include /etc/nginx/mime.types;
default_type application/octet-stream;
include /etc/nginx/conf.d/*.conf;
upstream {
#見upstream配置
}
server {
#見server配置
}
}
upstream配置
upstream mysvr {
#設定負載均衡的服務器列表
#weigth參數表示權值,權值越高被分配到的機率越大,如果權重都相同,weight可以都不設置
server 192.168.8.1:80 weight=5;
server 192.168.8.2:80 weight=1;
server 192.168.8.3:80 weight=6;
}
server配置
server {
listen 80; #偵聽80端口
listen 443 ssl; #偵聽443端口
server_name www.xxx.com; #定義使用www.xx.com訪問
#https證書
ssl_certificate /etc/ssl/certs/www.xxx.com_bundle.crt;
ssl_certificate_key /etc/ssl/certs/www.xxx.com.key;
error_page 404 /html/404.html;
error_page 500 502 503 504 /html/50x.html;
#設置主頁
location = / {
root /data/static/html;
index index.html;
}
#靜態文件,nginx自己處理
location ^~ /(html|js|css|jpg|png|txt|json)/ {
root /data/static/;
#過期30天,靜態文件不怎麼更新,過期可以設大一點,如果頻繁更新,則可以設置得小一點。
expires 30d;
#expires 15s;
}
#api轉發到後端服務器
location / {
proxy_pass http://mysvr/; #mysvr是上面配置的upstream
#以下是一些反向代理的配置可刪除.
#後端的Web服務器可以通過X-Forwarded-For獲取用戶真實IP
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m; #允許客戶端請求的最大單文件字節數
client_body_buffer_size 128k; #緩衝區代理緩衝用戶端請求的最大字節數,
proxy_connect_timeout 90; #nginx跟後端服務器連接超時時間(代理連接超時)
proxy_send_timeout 90; #後端服務器數據回傳時間(代理髮送超時)
proxy_read_timeout 90; #連接成功後,後端服務器響應時間(代理接收超時)
proxy_buffer_size 4k; #設置代理服務器(nginx)保存用戶頭信息的緩衝區大小
proxy_buffers 4 32k; #proxy_buffers緩衝區,網頁平均在32k以下的話,這樣設置
proxy_busy_buffers_size 64k; #高負荷下緩衝大小(proxy_buffers*2)
proxy_temp_file_write_size 64k; #設定緩存文件夾大小,大於這個值,將從upstream服務器傳
proxy_redirect off;
proxy_intercept_errors on;
}
}
location配置語法
語法規則: location [=|~|~*|^~] /uri/ { … }
=
開頭表示精確匹配^~
開頭表示uri以某個常規字符串開頭,理解爲匹配 url路徑即可。nginx不對url做編碼,因此請求爲/static/20%/aa,可以被規則^~ /static/ /aa匹配到(注意是空格)。~
開頭表示區分大小寫的正則匹配~*
開頭表示不區分大小寫的正則匹配!~
和!~*
分別爲區分大小寫不匹配及不區分大小寫不匹配 的正則/
通用匹配,任何請求都會匹配到。
多個location配置的情況下匹配順序爲(參考資料而來,還未實際驗證,試試就知道了,不必拘泥,僅供參考):
首先匹配 =,其次匹配^~, 其次是按文件中順序的正則匹配,最後是交給 / 通用匹配。當有匹配成功時候,停止匹配,按當前匹配規則處理請求。
例子,有如下匹配規則:
location = / {
#規則A
}
location = /login {
#規則B
}
location ^~ /static/ {
#規則C
}
location ~ \.(gif|jpg|png|js|css)$ {
#規則D
}
location ~* \.png$ {
#規則E
}
location !~ \.xhtml$ {
#規則F
}
location !~* \.xhtml$ {
#規則G
}
location / {
#規則H
}
那麼產生的效果如下:
訪問根目錄/, 比如http://localhost/ 將匹配規則A
訪問 http://localhost/login 將匹配規則B,http://localhost/register 則匹配規則H
訪問 http://localhost/static/a.html 將匹配規則C
訪問 http://localhost/a.gif, http://localhost/b.jpg 將匹配規則D和規則E,但是規則D順序優先,規則E不起作用, 而 http://localhost/static/c.png 則優先匹配到 規則C
訪問 http://localhost/a.PNG 則匹配規則E, 而不會匹配規則D,因爲規則E不區分大小寫。
訪問 http://localhost/a.xhtml 不會匹配規則F和規則G,http://localhost/a.XHTML不會匹配規則G,因爲不區分大小寫。規則F,規則G屬於排除法,符合匹配規則但是不會匹配到,所以想想看實際應用中哪裏會用到。
訪問 http://localhost/category/id/1111 則最終匹配到規則H,因爲以上規則都不匹配,這個時候應該是nginx轉發請求給後端應用服務器,比如FastCGI(php),tomcat(jsp),nginx作爲方向代理服務器存在。
所以實際使用中,個人覺得至少有三個匹配規則定義,如下:
#直接匹配網站根,通過域名訪問網站首頁比較頻繁,使用這個會加速處理,官網如是說。
#這裏是直接轉發給後端應用服務器了,也可以是一個靜態首頁
# 第一個必選規則
location = / {
proxy_pass http://tomcat:8080/index
}
# 第二個必選規則是處理靜態文件請求,這是nginx作爲http服務器的強項
# 有兩種配置模式,目錄匹配或後綴匹配,任選其一或搭配使用
location ^~ /static/ {
root /webroot/static/;
}
location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ {
root /webroot/res/;
}
#第三個規則就是通用規則,用來轉發動態請求到後端應用服務器
#非靜態文件請求就默認是動態請求,自己根據實際把握
#畢竟目前的一些框架的流行,帶.php,.jsp後綴的情況很少了
location / {
proxy_pass http://tomcat:8080/
}
nginx的其他配置信息介紹
三、ReWrite語法
last
– 基本上都用這個Flag。
break
– 中止Rewirte,不在繼續匹配
redirect
– 返回臨時重定向的HTTP狀態302
permanent
– 返回永久重定向的HTTP狀態301
1、下面是可以用來判斷的表達式:
-f
和!-f
用來判斷是否存在文件
-d
和!-d
用來判斷是否存在目錄
-e
和!-e
用來判斷是否存在文件或目錄
-x
和!-x
用來判斷文件是否可執行
2、下面是可以用作判斷的全局變量
例:http://localhost:88/test1/test2/test.php
$host:localhost
$server_port:88
$request_uri:http://localhost:88/test1/test2/test.php
$document_uri:/test1/test2/test.php
$document_root:D:\nginx/html
$request_filename:D:\nginx/html/test1/test2/test.php
四、Redirect語法
server {
listen 80;
server_name start.igrow.cn;
index index.html index.php;
root html;
if ($http_host !~ "^star\.igrow\.cn$" {
rewrite ^(.*) http://star.igrow.cn$1 redirect;
}
}
五、防盜鏈
location ~* \.(gif|jpg|swf)$ {
valid_referers none blocked start.igrow.cn sta.igrow.cn;
if ($invalid_referer) {
rewrite ^/ http://$host/logo.png;
}
}
六、根據文件類型設置過期時間
location ~* \.(js|css|jpg|jpeg|gif|png|swf)$ {
if (-f $request_filename) {
expires 1h;
break;
}
}
七、禁止訪問某個目錄
location ~* \.(txt|doc)${
root /data/www/wwwroot/linuxtone/test;
deny all;
}
附:一些可用的全局變量
$args
$content_length
$content_type
$document_root
$document_uri
$host
$http_user_agent
$http_cookie
$limit_rate
$request_body_file
$request_method
$remote_addr
$remote_port
$remote_user
$request_filename
$request_uri
$query
常用示例
根據請求參數過濾請求($args,$arg_xxx)
location = /rushBuy {
proxy_pass http://127.0.0.1:9000/;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_intercept_errors on;
if ($arg_timestamp ~* '\d*[0|2|4|6|8]$'){
add_header Content-Type 'application/json;charset=UTF-8';
return 200 '{"errCode": "-1","msg": "搶購失敗"}';
}
}
上述配置會攔截所有請求爲url:/rushBuy?userId=xxx&goodsId=xxx×tamp=xxx
中timestamp爲偶數的請求直接返回固定值,這樣可以限制一半的流量進入後臺服務器,在秒殺場景限流使用。
根據請求uri過濾請求($request_uri)
location ^~ /client/stat {
proxy_pass http:127.0.0.1:9000/;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $host;
proxy_intercept_errors on;
if ($request_uri ~* '/gmreport'){
return 200;
}
}