Home > Article > Operation and Maintenance > How to configure nginx page cache
nginx page cache
1. Instruction description
proxy_cache_path
Syntax: proxy_cache_path path [levels=number] keys_zone=zone_name:zone_size [inactive=time] [max_size=size];
Default value: none
Use fields : http
directive specifies the cache path and some other parameters. The cached data is stored in the file, and the hash value of the proxy url is used as the key and file name. The levels parameter specifies the number of cached subdirectories, for example:
proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=one:10m;
The file name is similar to:
/data/nginx/cache/c/29/b7f54b2df7773722d382f4809d65029c
levels specifies the directory structure. You can use any 1 or 2 digit number as the directory structure, such as x, x:x, or x:x:x For example: "2", "2:2", "1:1:2", but it can only be a third-level directory at most.
All active keys and metadata are stored in the shared memory pool. This area is specified with the keys_zone parameter. One refers to the name of the shared pool, and 10m refers to the size of the shared pool.
Note that each defined memory pool must have a unique path, for example:
proxy_cache_path /data/nginx/cache/one levels=1 keys_zone=one:10m; proxy_cache_path /data/nginx/cache/two levels=2:2 keys_zone=two:100m; proxy_cache_path /data/nginx/cache/three levels=1:1:2 keys_zone=three:1000m;
If the cached data is not requested within the time specified by the inactive parameter, it will be deleted. The default inactive is 10 minutes. A process called cache manager controls the cache size of the disk. It is used to delete inactive caches and control the cache size. These are defined in the max_size parameter. When the current cache value exceeds the value specified by max_size, it exceeds its size. The last least used data (LRU replacement algorithm) will be deleted. The size of the memory pool is set in proportion to the number of cached pages. The metadata size of a page (file) is determined by the operating system. For example, it is 64 bytes under freebsd/i386 and 128 bytes under freebsd/amd64.
proxy_cache
Syntax: proxy_cache zone_name;
Default value: none
Use fields: http, server, location
Set the name of a cache area. The same area can be used in different places.
After 0.7.48, the cache follows the "expires", "cache-control: no-cache", "cache-control: max-age=xxx" header fields of the backend, 0.7. After version 66, "cache-control: "private" and "no-store" headers are also followed. nginx will not process the "vary" header during the caching process. In order to ensure that some private data is not seen by all users, later The client must set the "no-cache" or "max-age=0" header, or the proxy_cache_key contains user-specified data such as $cookie_xxx. Using the cookie value as part of the proxy_cache_key can prevent private data from being cached, so it can be used in different locations. Specify the value of proxy_cache_key separately to separate private data and public data.
The caching directive relies on proxy buffers (buffers). If proxy_buffers is set to off, the cache will not take effect.
proxy_cache_valid
Syntax: proxy_cache_valid reply_code [reply_code …] time;
Default value: none
Use fields: http, server, location
Set different cache times for different responses, for example:
proxy_cache_valid 200 302 10m; proxy_cache_valid 404 1m;
Set the cache time to 10 minutes for response codes 200 and 302, 404 code Cache for 1 minute.
If you only define time:
proxy_cache_valid 5m;
Then only responses with codes 200, 301 and 302 will be cached.
Similarly You can use any parameter for any response.
proxy_cache_valid 200 302 10m; proxy_cache_valid 301 1h; proxy_cache_valid any 1m;
2. Define a simple nginx cache server
[root@nginx ~]# vim /etc/nginx/nginx.conf proxy_cache_path /data/nginx/cache/webserver levels=1:2 keys_zone=webserver:20m max_size=1g; server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { proxy_pass http://webservers; proxy_set_header x-real-ip $remote_addr; proxy_cache webserver; proxy_cache_valid 200 10m; } }
3. Create a new cache directory
[root@nginx ~]# mkdir -pv /data/nginx/cache/webserver
4. Reload the configuration File
[root@nginx webserver]# service nginx reload nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful 重新载入 nginx: [确定]
5. Let’s test it (Google Chrome)
Note, when you test with Google Chrome, you can Press f12 to call the development tool and select the network option. We can see the response headers. Here we can see whether we are requesting cache, but we can’t see it yet. Let’s configure it and test it again.
6. Cache variable description
$server_addr
Server address, this value can be determined after completing a system call, if you want to bypass System call, you must specify the address in listen and use the bind parameter.
$upstream_cache_status
In version 0.8.3, its value may be:
miss Missed
expired - expired. The request is sent to the backend.
updating - expired. Because proxy/fastcgi_cache_use_stale is being updated, the older version of the response will be used.
stale - expired. When using proxy/fastcgi_cache_use_stale, the backend receives stale responses.
hit Hit
[root@nginx ~]# vim /etc/nginx/nginx.conf proxy_cache_path /data/nginx/cache/webserver levels=1:2 keys_zone=webserver:20m max_size=1g; server { listen 80; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; #增加两头部 add_header x-via $server_addr; add_header x-cache $upstream_cache_status; location / { proxy_pass http://webservers; proxy_set_header x-real-ip $remote_addr; proxy_cache webserver; proxy_cache_valid 200 10m; } }
7. Reload the configuration file
[root@nginx ~]# service nginx reload nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful 重新载入 nginx: [确定]
8. Test it
注,从图中我们可以看到,我们访问的服务器是192.168.18.208,缓存命中。大家可以看到是不是很直观啊。下面我们看一下缓存目录。
9.查看一下缓存目录
[root@nginx ~]# cd /data/nginx/cache/webserver/f/63/ [root@nginx 63]# ls 681ad4c77694b65d61c9985553a2763f
注,缓存目录里确实有缓存文件。
The above is the detailed content of How to configure nginx page cache. For more information, please follow other related articles on the PHP Chinese website!