修改 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时写日志