Nginx缓存
一、基于proxy_cache的缓存
# 自动匹配CPU核心数 worker_processes auto; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for" "$upstream_cache_status"'; # 指定缓存文件路径选项只允许放在http标签下 # 缓存文件名命名规则/usr/local/nginx/cache/c/29/b7f54b2df7773722d382f4809d65029c # use_temp_path=off 取消单独proxy_temp_path,性能更好 proxy_cache_path /usr/local/nginx/cache levels=1:2 keys_zone=cache_zone:50m inactive=1d max_size=1g use_temp_path=off; # 代理大响应时,磁盘中转临时文件存放目录,配置了use_temp_path=off后proxy_temp_path不需要配置 # proxy_temp_path /usr/local/nginx/cache_temp; # 缓冲配置:用于存放后端响应头 + 响应第一块数据 proxy_buffer_size 64k; # 额外缓冲池:4个缓冲区,每个128k,总共 4*128=512k,总内存缓冲容量 = 64k + 512k = 576KB proxy_buffers 4 128k; # 最大临时文件阈值:响应总大小≤1G全部放内存;>1G才写入到cache_temp目录里 proxy_max_temp_file_size 1G; server { listen 80; server_name localhost; access_log logs/access.log main; location / { # 启用上面定义好的cache_zone缓存区 proxy_cache cache_zone; # 自定义缓存key proxy_cache_key "$scheme$proxy_host$request_uri"; # 301/302/304状态码缓存24小时,配置多个proxy_cache_valid时匹配顺序是从上到下匹配 proxy_cache_valid 301 302 304 24h; # 200状态码缓存5分钟,时间单位支持:s(秒)/m(分)/h(时)/d(天) proxy_cache_valid 200 5m; # 其余状态码不缓存 proxy_cache_valid any 0; # 防缓存击穿 proxy_cache_lock on; proxy_cache_lock_timeout 5s; # 后端异常返回过期缓存 proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503; proxy_pass http://192.168.0.37/; # 完整代理请求头 proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Proto $scheme; proxy_http_version 1.1; proxy_set_header Connection ""; # 在http header上增加缓存命中显示,不加always只在2xx成功响应返回,500/404看不到缓存状态 add_header Nginx-Cache "$upstream_cache_status" always; # 浏览器客户端缓存30分钟 expires 30m; } } }
proxy_cache_path /usr/local/nginx/cache levels=1:2 keys_zone=cache_zone:50m inactive=1d max_size=1g;
# proxy_cache_path,表示缓存文件存放的路径,该路径是预先就要创建好的
# levels=1:2,设置在相对于path指定目录的第几级hash目录中缓存数据
# levels=1,表示一级hash目录,levels=1:2,表示两级hash目录,目录的名称是基于请求URL通过哈希#算法得到的
# keys_zone=cache_zone:50m,设置缓存区名称为cache_one,内存缓存为50MB
# inactive=1d,自动清除1天内没有被访问的文件
# max_size=1g,设置硬盘中缓存数据的最大缓存空间为1G
代理缓存参数
client_body_buffer_size 512k; # 缓冲区代理缓冲客户端请求的最大字节数
proxy_connect_timeout 60; # 连接后端服务器超时时间
proxy_read_timeout 60; # 后端服务器响应请求超时时间
proxy_send_timeout 60; # 后端服务器发送数据超时时间
proxy_busy_buffers_size 128k; # 系统繁忙时可申请的proxy_buffers大小(proxy_buffers*2)
proxy_temp_file_write_size 128k; # proxy缓存临时文件的大小
$upstream_cache_status包含以下几种状态:
MISS:未命中,请求被传送到后端
HIT:缓存命中
EXPIRED:缓存已经过期请求被传送到后端
UPDATING:正在更新缓存,将使用旧的应答
STALE:后端将得到过期的应答
添加到http头中后通过curl或浏览器查看http header如下:
HTTP/1.1 200 OK
Date: Mon, 22 Apr 2013 02:10:02 GMT
Server: nginx
Content-Type: image/jpeg
Content-Length: 23560
Last-Modified: Thu, 18 Apr 2013 11:05:43 GMT
Nginx-Cache: HIT
Accept-Ranges: bytes
Vary: User-Agent
添加到nginx日志中
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" "$upstream_cache_status"';
二、基于proxy_store的缓存
worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; } location / { root "/web1"; proxy_store on; proxy_store_access user:rw group:rw all:r; proxy_temp_path "/web1"; if ( !-f $request_filename ) { proxy_pass http://192.168.0.37; } } }
通过location的 if 条件判断驱动nginx代理服务器与后端服务器通信和web缓存;判断请求的资源在nginx代理服务器上是否存在,如果不存在就通过后端服务器获取数据,然后回传给客户端,同时使用proxy store进行缓存。
用户一访问的时候,主目录为/web1,如果里面没有用户需要的数据,就去代理取,然后缓存到自己的主目录,下次再访问同样的资源的时候,就能在缓存这里得到数据
创建文件夹:mkdir /web1
proxy store 与proxy cache的区别:proxy store不提供缓存过期更新,内存索引建立等功能,缓存文件一直会保存在本地磁盘中
三、nginx限流配置和灰度发布配置
# nginx限流配置,rate=10r/s:每秒每IP允许10个请求,burst=20:允许突发20个请求排队,nodelay:突发请求立即处理,不等待,limit_conn perip 10:每IP同时最多10个连接 limit_req_zone $binary_remote_addr zone=req_limit:10m rate=10r/s; server { location /api { limit_req zone=req_limit burst=20 nodelay; limit_conn perip 10; proxy_pass http://backend; } } # nginx灰度发布配置,10%的流量落到backend_v2,其余走backend_v1。观察一段时间没问题,把比例调到100%就完成全量。比硬切风险小很多 split_clients "${remote_addr}${request_uri}" $upstream_pool { 10% "backend_v2"; * "backend_v1"; } upstream backend_v1 { server 192.168.1.10:3000; } upstream backend_v2 { server 192.168.1.11:3000; } server { location / { proxy_pass http://$upstream_pool; } }
参考链接:
https://www.cnblogs.com/lemon-le/p/7804347.html
浙公网安备 33010602011771号