I. Proxy module caching
1. Configuration files
#注:proxy_temp_path和proxy_cache_path指定的路径必须在同一分区proxy_temp_path /web/tmp/nginx;#设置Web缓存区名称为cache_one,内存缓存空间大小为100MB,1天没有被访问的内容自动清除,硬盘缓存空间大小为1GB。proxy_cache_path /web/cache/nginx levels=1:2 keys_z>100m inactive=1d max_size=1g;
location / { #如果后端的服务器返回502、504、执行超时等错误,自动将请求转发到upstream负载均衡池中的另一台服务器,实现故障转移。 proxy_next_upstream http_502 http_504 error timeout invalid_header; proxy_cache cache_one; #对不同的HTTP状态码设置不同的缓存时间 2003041h; #以域名、URI、参数组合成Web缓存的Key值,Nginx根据Key值哈希,存储缓存内容到二级缓存目录内 $host$uri$is_args$args; proxy_pass http://webserver; proxy_redirect off; proxy_set$host; proxy_set$remote_addr; proxy_set$proxy_add_x_forwarded_for; }
The caching feature of the reverse proxy is a redirect, not the ability to cache static files based on URL generation
Two. fastcgi Module cache
1. Configuration files
fastcgi_temp_path /web/tmp/fastcgi;#设置fastcgi缓存路径 levels代表目录层级,1:2会生成16*256,2:2会生成256*256 keys_zone代表缓冲区名称 inactive代表过期时间 max_size代表最多用多少磁盘空间fastcgi_cache_path /web/cache/fastcgi levels=1:2 keys_z>100m inactive=1d max_size=1g;
Location~ [^/]\.php (/|$){Fastcgi_cacheCache_two;Fastcgi_cache_valid $10m;Fastcgi_cache_methodsGET HEAD;#忽视以下头信息fastcgi_ignore_headers"Cache-control""Expires""Set-cookie";Fastcgi_cache_key"$scheme$request _method$host$request _uri";#添加状态头信息Add_headerX-cache-cfc"$upstream _cache_status - $upstream _response_time";Fastcgi_indexindex.php;Fastcgi_pass127.0.0.1:9000;includeFastcgi_params;Fastcgi_split_path_info^ (. +?\.php) (/.*) $;Fastcgi_paramScript_filename$document _root$fastcgi _script_name;Fastcgi_paramScript_name$fastcgi _script_name;Fastcgi_paramPath_info$fastcgi _path_info;Fastcgi_paramPath_translated$document _root$fastcgi _path_info; }
Attention:
1. If the FASTCGI response header message has expires Cache-control Set-cookie, Fastcgi_cache is not working, so you need to add fastcgi_ignore_headers this configuration.
2. Add header information X-CACHE-CFC is to test whether the cache works, $upstream _cache_status contains the following states:
MISS miss, request is routed to backend
Hit Cache Hits
EXPIRED Cache has expired request sent to backend
UPDATING is updating the cache and will use the old answer
STALE back end will get an expired reply
The BYPASS cache is bypassed
Three. Reference
Http://www.qttc.net/201307355.html
https://serversforhackers.com/nginx-caching/
Http://www.ha97.com/5194.html
http://www.cnxct.com/several-reminder-in-nginx-fastcgi_cache-and-php-session_cache_limiter/
The above describes the Nginx series (14 Nginx cache), including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.