: This article mainly introduces the Nginx series (14 nginx cache). If you are interested in the PHP Tutorial, refer to it. I. proxy module cache
1. configuration file
# Note: the paths specified by proxy_temp_path and proxy_cache_path must be in the same partition: proxy_temp_path/web/tmp/nginx; # set the Web cache zone name to cache_one and the memory cache space to 100 MB, the content that is not accessed within one day is automatically cleared, and the size of the hard disk cache space is 1 GB. Proxy_cache_path/web/cache/nginx levels = keys_z> 100 m inactive = 1d max_size = 1g;
Location/{# if the backend server returns errors such as 502, 504, and execution timeout, the request is automatically forwarded to another server in the upstream server load balancer pool for failover. Proxy_next_upstream http_502 http_504 error timeout invalid_header; proxy_cache cache_one; # set different cache times for different HTTP status codes proxy_cache_valid 2003041 h; # combine domain names, Uris, and parameters into the Web cache Key values. Nginx stores the cached content to the proxy_cache_key $ host $ URI $ is_args $ args in the second-level cache directory based on the hash of the Key values; proxy_pass http: // webserver; proxy_redirect off; proxy_set_header Host $ host; proxy_set_header X-Real-IP $ remote_addr; proxy_set_header X-Forwarded-For $ response ;}
The cache function of the reverse proxy is a redirection, not a function for generating cached static files based on URLs.
II. fastcgi module cache
1. configuration file
Fastcgi_temp_path/web/tmp/fastcgi; # set the fastcgi cache path levels to represent the directory level. at, 16*256, 2 will be generated: 2. 256*256 keys_zone is generated. the buffer name inactive indicates the Expiration Time. max_size indicates the maximum disk space used. fastcgi_cache_path/web/cache/fastcgi levels = keys_z> 100 m inactive = 1d max_size. = 1g;
Location ~ [^/] \. Php (/| $) {fastcgi_cache cache_two; fail; fastcgi_cache_methods get head; # ignore the following header information: fastcgi_ignore_headers "Cache-Control" "Expires" "Set-Cookie "; fastcgi_cache_key "$ scheme $ request_method $ host $ request_uri"; # Add status header information add_header X-Cache-CFC "$ upstream_cache_status-$ upstream_response_time"; fastcgi_index index. php; fastcgi_pass127.0.0.1: 9000; include fastcgi_params; fastcgi_spl It_path_info ^ (. +? \. Php) (/. *) $; fastcgi_param SCRIPT_FILENAME $ document_root $ response; fastcgi_param SCRIPT_NAME $ response; fastcgi_param PATH_INFO $ response; fastcgi_param PATH_TRANSLATED $ document_root $ response ;}
Note:
1. if the header of the fastcgi response contains Expires Cache-Control Set-Cookie, fastcgi_cache does not work. Therefore, you need to add the fastcgi_ignore_headers configuration item.
2. add the header information X-Cache-CFC to test whether the Cache works. $ upstream_cache_status contains the following states:
MISS missed, the request is sent to the backend
HIT cache HIT
An EXPIRED Cache EXPIRED request is sent to the backend
UPDATING is UPDATING the cache. the old response will be used.
The STALE backend will receive an expired response.
BYPASS cache is bypassed
III. 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 introduces the Nginx series (14 nginx cache), including some content, hope to be helpful to friends who are interested in PHP tutorials.