Nginx Cache
Nginx has two types of caching mechanisms: Fastcgi_cache and Proxy_cache
Let's talk about the difference between the two caching mechanisms.
The Proxy_cache function is to cache the contents of the backend server, possibly any content, including static and dynamic
Fastcgi_cache function is the cache fastcgi generated content, many cases are PHP generated dynamic content
The Proxy_cache cache reduces the number of nginx and back-end communications, saving transmission time and back-end bandwidth
The Fastcgi_cache cache reduces the number of nginx and PHP traffic, and reduces the pressure on PHP and the database.
Proxy_cache Cache Settings
#注: The paths specified by Proxy_temp_path and Proxy_cache_path must be proxy_temp_path/data0/proxy_temp_dir in the same partition;
#设置Web缓存区名称为cache_one, the memory cache space size is 200mb,1 Day has not been accessed automatically clear, hard disk cache space size of 30GB.
Proxy_cache_path/data0/proxy_cache_dir levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;
server {Listen server_name www.yourdomain.com 192.168.8.42; index index.html index.htm; root/data0/htdocs/www;
Location/{#如果后端的服务器返回502, 504, execution timeout error, automatically forward the request to another server in the upstream load balancing pool for failover.
Proxy_next_upstream http_502 http_504 error timeout invalid_header;
Proxy_cache Cache_one;
#对不同的HTTP状态码设置不同的缓存时间 proxy_cache_valid 304 12h;
#以域名, URI, and parameters are combined into the Web cache key value, nginx the cache content to the level two cache directory Proxy_cache_key $host $uri$is_args$args According to the key value hash;
Proxy_set_header Host $host;
Proxy_set_header x-forwarded-for $remote _addr;
Proxy_pass Http://backend_server;
Expires 1d;
#用于清除缓存, assuming that a URL is http://192.168.8.42/test.txt, you can clear the cache of the URL by accessing the http://192.168.8.42/purge/test.txt.
Location ~/purge (/.*) {#设置只允许指定的IP或IP段才可以清除URL缓存.
Allow 127.0.0.1; Allow 192.168.0.0/16;
Deny all;
Proxy_cache_purge Cache_one $host $1$is_args$args;
Dynamic applications that end #扩展名以. PHP,. JSP,. CGI are not cached. Location ~. *\. (php|jsp|cgi) $ {proxy_set_header Host $host; proxy_set_header x-forwarded-for $remote _addr; Proxy_pass http://backend
_server;
} Access_log off; }
}
Fastcgi_cache Cache Settings
#定义缓存存放的文件夹
fastcgi_cache_path/tt/cache levels=1:2 keys_zone=name:2880m inactive=2d max_size=10g;
#定义缓存不同的url请求
fastcgi_cache_key "$scheme $request_method$host$uri$arg_filename$arg_x$arg_y";
server {
listen 8080;
server_name www.example. com;
Location/{
root/www;
Index index.html index.htm index.php;
}
Location ~ (|. PHP) $ {
root/www;
Fastcgi_pass 127.0.0.1:9000;
Fastcgi_cache NAME;
Fastcgi_cache_valid 48h;
Fastcgi_cache_min_uses 1;
Fastcgi_cache_use_stale error timeout Invalid_header http_500;
Fastcgi_index index.php;
Fastcgi_param Script_filename/scripts$fastcgi_script_name;
Include fastcgi.conf;
#设置缓存的过程中发现无法获取cookie, the need to define this sentence
Fastcgi_pass_header set-cookie;
}
Log_format access ' $remote _addr-$remote _user [$time _local] "$request" "$status
$body _bytes_sent" $http _referer "
" $http _user_agent "$http _x_forwarded_for";
Access_log/httplogs/access.log access;
}
Overall, Nginx's Proxy_cache and Fastcgi_cache cache configurations are similar.