修改 Response : sub_filterhtml
location / {node
sub_filter /blog/ /blog-staging/;nginx
sub_filter_once off;後端
}緩存
修改 Request Header : proxy_set_header服務器
location /some/path/ {cookie
proxy_set_header Host $host;app
proxy_set_header X-Real-IP $remote_addr;負載均衡
proxy_pass http://localhost:8000;tcp
}
指定特定ip訪問某個目錄 : proxy_bind
location /app1/ {
proxy_bind 127.0.0.1;
proxy_pass http://example.com/app1/;
}
默認狀況下,請求緩存的key是Request字符串( As the key (identifier) for a request, NGINX Plus uses the request string),也能夠使用變量設置Cache key:
proxy_cache_key "$host$request_uri$cookie_user";
設置key的最小使用次數,而後才生效
proxy_cache_min_uses 5; 最少使用5次後生效
使用GET Head 之外的方法作爲Cached,須要添加到配置裏: proxy_cache_methods
proxy_cache_methods GET HEAD POST;
根據 status codes 設置cache生效時長: proxy_cache_valid
proxy_cache_valid 200 302 10m;
proxy_cache_valid 404 1m;
proxy_cache_valid any 5m; //所有5分鐘
繞過cache proxy_cache_bypass
:
當最後個參數不爲空,0 。Ngx不檢查Cache,直接發送到後端服務器
proxy_cache_bypass $cookie_nocache $arg_nocache$arg_comment;
proxy_no_cache $http_pragma $http_authorization; //不響應全部請求Cache請求
tcp_nodelay,開啓是用於網速慢,將小包打包成大包發送,200ms延遲發送
默認是關閉狀態,只用於 keepalive
location /mp3 {
tcp_nodelay on;
keepalive_timeout 65;
...
}
Nginx 負載均衡 ,Load Balancing Method,有4種,Nginx push 支持5種
upstream backend {
server backend1.example.com weight=5;
server backend2.example.com;
server 192.0.0.1 backup;
}
6個請求 5個 backend1 一上backend2,backup在backend1和backend2不可用時會收到請求
upstream backend {
least_conn;
server backend1.example.com;
server backend2.example.com;
}
upstream backend {
ip_hash;
server backend1.example.com;
server backend2.example.com;
server backend3.example.com down; //刪除這個服務器不作Hash(下線了)
}
upstream backend {
hash $request_uri consistent;
server backend1.example.com;
server backend2.example.com;
}
Active Health Monitoring
location / {
proxy_pass http://backend;
health_check interval=10 fails=3 passes=2;
}
檢測間隔 10s,連續失敗3次認識服務器不可用,連續2次認識服務器可用
location / {
proxy_pass http://backend;
health_check uri=/some/path;
}
按指定的uri進行檢測,默認訪問 /
Limiting the Number of Connections ,須要使用 limit_conn_zone 定義規則
limit_conn_zone $binary_remote_address zone=addr:10m; 名稱是addr ,共享10m內存
location /download/ {
limit_conn addr 1;
}
Limiting the Request Rate ,須要使用 limit_req_zone
定義規則
limit_req_zone $binary_remote_addr zone=one:10m rate=1r/s; requests per second (r/s) or requests per minute (r/m)
location /search/ {
limit_req zone=one burst=5 nodelay;
}
大於請求速率會把請求放一個隊列延遲隊列裏,若是不須要使用 nodelay參數
burst 設置最大等待處理請求數,超過這個數據,服務器返回503
Limiting the Bandwidth ,能夠使用 limit_rate
作限制
location /download/ {
limit_rate 50k; 50kb/秒
}
每一個客戶端能夠打開多個連接進行下載
location /download/ {
limit_conn addr 1;
limit_rate_after 500k;
limit_rate 50k;
}
addr根據上面的例子的定義,每一個ip只能有一個鏈接,下載500k後.限制下載50k/秒,
error_log
錯誤級別: warn
, error
crit
, alert
, emerg
ccess_log
NGINX writes information about client requests in the access log right after the request is processed。請求處理完成後寫Access log
$request_time
– The total time spent processing a request
open_log_file_cache (寫日誌時使用緩存)
To enable caching of log file descriptors, use the open_log_file_cache
directive.
open_log_file_cache max=1000 inactive=20s valid=1m min_uses=2;
按條件寫日誌(Enabling Conditional Logging)
map $status $loggable {
~^[23] 0;
default 1;
}
access_log /path/to/access.log combined if=$loggable;
狀態爲非2xx,3xx時寫日誌