读Nginx官方文档笔记

 

修改 Response           : sub_filter

location / {

    sub_filter      /blog/ /blog-staging/;

    sub_filter_once off;

}

 

修改 Request Header : proxy_set_header

location /some/path/ {

    proxy_set_header Host $host;

    proxy_set_header X-Real-IP $remote_addr;

    proxy_pass http://localhost:8000;

}

 

指定特定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种

  1.  round-robin   默认 ,可以使用weight,weight 默认值为 1

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不可用时会收到请求

 

  1.  least_conn    最少活跃连接数

upstream backend {

    least_conn;

    server backend1.example.com;

    server backend2.example.com;

}

 

  1.  ip_hash  根据客户端IP 做Hash,保证每个请求都请求同一个服务器

upstream backend {

    ip_hash;

    server backend1.example.com;

    server backend2.example.com;

     server backend3.example.com down; //删除这个服务器不做Hash(下线了)

}

 

  1.  hash  指定一个key做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  错误级别:  warnerror critalertemerg 

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

 

转载于:https://my.oschina.net/u/1053317/blog/993840

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值