Nginx缓存
http{upstream cache {server 192.168.69.113:8081;server 192.168.69.113:8082;server 192.168.69.113:8083; }proxy_cache_path /soft/cache levels=1:2 keys_zone=code_cache:10m max_size=10g inactive=60m use_temp_path=off;server {listen 80;server_name 192.168.69.12;location / {proxy_pass http://cache;proxy_cache code_cache;proxy_cache_valid 200 304 12h;proxy_cache_valid any 10m;proxy_cache_key $host$uri$is_args$args;add_header Nginx-Cache "$upstream_cache_status";proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;include proxy_params;}}
proxy_cache_path /soft/cache levels=1:2 keys_zone=code_cache:10m max_size=10g inactive=60m use_temp_path=off;
#proxy_cache_path存放缓存临时⽂件
#levels 按照两层⽬录分级
#keys_zone 开辟空间名, 10m:开辟空间⼤⼩, 1m可存放8000key
#max_size 控制最⼤⼤⼩, 超过后Nginx会启⽤淘汰规则#inactive 60分钟没有被访问缓存会被清理
#use_temp_path 临时⽂件, 会影响性能, 建议关闭
proxy_cache code_cache;
- 开启proxy_cache,code_cache为proxy_cache_path中的keys_zone
proxy_cache_valid 200 304 12h;
- 状态码200|304的过期为12h
proxy_cache_valid any 10m;
- 其他状态码过期为10m
proxy_cache_key $host$uri$is_args$args;
- 给缓存数据定义一个键
add_header Nginx-Cache “$upstream_cache_status”;
- 增加头信息, 观察客户端response是否命中
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
- 出现500-504或错误, 会跳过此台服务器访问下台
删除缓存:删除/soft/cache中的内容即可
设置部分内容不缓存
if ($request_uri ~ ^/(url3|login|register|password)) {set $cookie_nocache 1;}location / {proxy_pass http://cache;proxy_cache code_cache;proxy_cache_valid 200 304 12h;proxy_cache_valid any 10m;proxy_cache_key $host$uri$is_args$args;#不缓存配置,如果至少有一个值的字符串参数不为空或不等于0,那么响应将不会被保存proxy_no_cache $cookie_nocache $arg_nocache $arg_comment;add_header Nginx-Cache "$upstream_cache_status";proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;include proxy_params;}
缓存互串
proxy_cache_key $host$uri$is_args$args;通过这样的配置,存在用户和用户间的缓存互串问题可以通过以下方式解决proxy_cache_key $http_cookie$uri$is_args$args;但是还有个问题,例如一个不会变的静态资源,这样的配置,来100个用户请求,这样就保存了至少100份总结:nginx缓存除了处理静态资源,其他还是谨慎使用
Rewrite
Syntax: rewrite regex replacement [flag];Default: --Context: server, location, if//所有请求转发⾄/pages/maintain.htmlrewrite ^(.*)$ /pages/maintain.html break;

location ~ ^/break{rewrite ^/break /test/json break;}location ~ ^/last{rewrite ^/last /test/josn last;}location /test/{default_type application/json;return 200 '{"status":"success"}';}127.0.0.1/break >>>404127.0.0.1/last >>>{"status":"success"}
last 与 break 对⽐总结:
last会将replacement作为一个新请求,然后再去匹配localtion
break不会发起新请求,replacement如果已经定位到资源就返回,没有返回404
server {listen 80;server_name bgx.com;rewrite ^ http://www.bgx.com$request_uri?;}server {listen 80;server_name www.bgx.com;}
Return
示例1:server{listen 80;server_name www.aming.com;return 403;..... //下面所有都不会执行}示例2:server {.....if ($request_uri ~ "\.htpasswd|\.bak"){return 404;rewrite /(.*) /aaa.txt; //该行配置不会被执行。}//如果下面还有其他配置,会被执行。.....}
示例3:server{listen 80;server_name www.aming.com;return 200 "hello";}说明:如果要想返回字符串,必须要加上状态码,否则会报错。还可以支持json数据示例4:location ^~ /aming {default_type application/json ;return 200 '{"name":"aming","id":"100"}';}也支持写一个变量示例5:location /test {return 200 "$host $request_uri";}
server{listen 80;server_name www.aming.com;return 301 http://www.aminglinux.com/123.html;}注意:return后面的url必须是以http://或者https://开头的。
