nginx有两种缓存机制:fastcgi_cache和proxy_cache
nginx有两种缓存机制:fastcgi_cache和proxy_cache<br />下面我们来说说这两种缓存机制的区别吧<br />
proxy_cache作用是缓存后端服务器的内容,可能是任何内容,包括静态的和动态的<br />
fastcgi_cache作用是缓存fastcgi生成的内容,很多情况是php生成的动态内容<br />
proxy_cache缓存减少了nginx与后端通信的次数,节省了传输时间和后端带宽<br />
fastcgi_cache缓存减少了nginx与php的通信次数,更减轻了php和数据库的压力。<br />
<br />
proxy_cache缓存设置<br />
#注:proxy_temp_path和proxy_cache_path指定的路径必须在同一分区<br />
proxy_temp_path /data0/proxy_temp_dir;<br />
#设置Web缓存区名称为cache_one,内存缓存空间大小为200MB,1天没有被访问的内容自动清除,硬盘缓存空间大小为30GB。
<pre>
proxy_cache_path/data0/proxy_cache_dirlevels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;
server
{
listen 80;
server_namewww.yourdomain.com 192.168.8.42;
index index.html index.htm;
root/data0/htdocs/www;
location /
{
#如果后端的服务器返回502、504、执行超时等错误,自动将请求转发到upstream负载均衡池中的另一台服务器,实现故障转移。
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_cache cache_one;
#对不同的HTTP状态码设置不同的缓存时间
proxy_cache_valid200 304 12h;
#以域名、URI、参数组合成Web缓存的Key值,Nginx根据Key值哈希,存储缓存内容到二级缓存目录内
proxy_cache_key $host$uri$is_args$args;
proxy_set_header Host$host;
proxy_set_header X-Forwarded-For$remote_addr;
proxy_pass http://backend_server;
expires 1d;
}
#用于清除缓存,假设一个URL为http://192.168.8.42/test.txt,通过访问http://192.168.8.42/purge/test.txt就可以清除该URL的缓存。
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;
}
#扩展名以.php、.jsp、.cgi结尾的动态应用程序不缓存。
location ~ .*.(php|jsp|cgi)?$
{
proxy_set_header Host$host;
proxy_set_header X-Forwarded-For$remote_addr;
proxy_pass http://backend_server;
}
access_logoff;
}
}</pre>
<br />
fastcgi_cache缓存设置<br />
#定义缓存存放的文件夹<br />
fastcgi_cache_path /tt/cache levels=1:2 keys_zone=NAME:2880m inactive=2d max_size=10G;<br />
<br />
#定义缓存不同的url请求
<pre>
fastcgi_cache_key "$scheme$request_method$host$uri$arg_filename$arg_x$arg_y";
server {
listen 8080;
server_namewww.example .com;
location / {
root /www;
indexindex.html index.htm index.php;
}
location ~ (|.php)$ {
root /www;
fastcgi_pass 127.0.0.1:9000;
fastcgi_cache NAME;
fastcgi_cache_valid 200 48h;
fastcgi_cache_min_uses1;
fastcgi_cache_use_stale errortimeout invalid_header http_500;
fastcgi_indexindex.php;
fastcgi_paramSCRIPT_FILENAME/scripts$fastcgi_script_name;
include fastcgi.conf;
#设置缓存的过程中发现无法获取cookie,经查需要定义这句话
fastcgi_pass_header Set-Cookie;
}
log_formataccess'$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
access_log/httplogs/access.logaccess;
}</pre>
总的来说 nginx的proxy_cache和fastcgi_cache的缓存配置差不多。 <p>nginx不仅有个大家很熟悉的缓存代理后端内容的proxy_cache,还有个被很多人忽视的fastcgi_cache。<br />
proxy_cache的作用是缓存后端服务器的内容,可能是任何内容,包括静态的和动态。<br />
fastcgi_cache的作用是缓存fastcgi生成的内容,很多情况是php生成的动态的内容。<br />
proxy_cache缓存减少了nginx与后端通信的次数,节省了传输时间和后端宽带。<br />
fastcgi_cache缓存减少了nginx与php的通信的次数,更减轻了php和数据库(mysql)的压力,这比用memcached之类的缓存要轻松得多。<br />
但是,缓存也有弊端,比如说评论了之后不能会当时显示等等,自己取舍,有得必有失。<br />
<br />
本着测试的原则,在本站上测试了一下fastcgi_cache这个功能,,貌似还不错,减少数据库的查询了,顺便优化下fastcgi的参数,的嘿嘿,,贴上配置nginx的httpd段里面加入</p>
<p>fastcgi_connect_timeout 300;<br />
指定连接到后端FastCGI的超时时间。<br />
fastcgi_send_timeout 300;<br />
向FastCGI传送请求的超时时间,这个值是指已经完成两次握手后向FastCGI传送请求的超时时间。<br />
fastcgi_read_timeout 300;<br />
接收FastCGI应答的超时时间,这个值是指已经完成两次握手后接收FastCGI应答的超时时间。<br />
fastcgi_buffer_size 32k;<br />
指定读取FastCGI应答第一部分需要用多大的缓冲区,一般第一部分应答不会超过1k,由于页面大小为4k,所以这里设置为4k。<br />
fastcgi_buffers 4 32k;<br />
定本地需要用多少和多大的缓冲区来缓冲FastCGI的应答请求。如果一个PHP脚本所产生的页面大小为256KB,那么会为其分配4个64KB的缓冲区来缓存;如果页面大小大于256KB,那么大于256KB的部分会缓存到fastcgi_temp指定的路径中,但是这并不是好方法,因为内存中的数据处理速度要快于硬盘。一般这个值应该为站点中PHP脚本所产生的页面大小的中间值,如果站点大部分脚本所产生的页面大小为256KB,那么可以把这个值设置为“16 16k”、“4 64k”等。<br />
fastcgi_busy_buffers_size 64k;<br />
默认值是fastcgi_buffers的两倍。<br />
fastcgi_temp_file_write_size 64k;<br />
在写入fastcgi_temp_path时将用多大的数据块,默认值是fastcgi_buffers的两倍。<br />
fastcgi_cache_path /var/logs/nginx/fastcgi_cache_dir levels=1:2 keys_zone=cache_fastcgi:128m inactive=1d max_size=10g;<br />
这个指令为FastCGI缓存指定一个路径,目录结构等级,关键字区域存储时间和非活动删除时间。以及最大占用空间。</p>
<p>然后在server的location里面调用,如下缓存php文件:</p>
<p>location ~ \.php$ {<br />
root /var/www/html/yan_blog;<br />
fastcgi_pass 127.0.0.1:9000;<br />
fastcgi_index index.php;<br />
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;<br />
include fastcgi_params;<br />
fastcgi_cache cache_fastcgi;<br />
表示开启FastCGI缓存并为其指定一个名称。<br />
fastcgi_cache_valid 200 302 301 1h;<br />
fastcgi_cache_valid any 1m;<br />
为指定的应答代码指定缓存时间,如上例中将200,302 301应答缓存一小时,其他为1分钟。<br />
fastcgi_cache_min_uses 1;<br />
设置链接请求几次就被缓存。<br />
fastcgi_cache_use_stale error timeout invalid_header http_500;<br />
定义哪些情况下用过期缓存<br />
fastcgi_cache_key $request_method://$host$request_uri;<br />
注意一定要加上$request_method作为cache key,否则如果HEAD类型的先请求会导致后面的GET请求返回为空<br />
}</p>
<p>设置了之后重启nginx就可以生效了,这个时候再访问php的页面的话,就会被缓存了,可以查看/var/logs/nginx/fastcgi_cache_dir这个目录下面是有缓存文件的。最后再说明一点,如果更改了缓存目录的路径,一定要把缓存的名称也改掉,后端调用的名称也同步改掉,如果只改掉了缓存目录,不改缓存名称的话,缓存的时候还是会缓存到之前的路径下面去,但是调用的时候调用的是新的路径,这个时候就会出现找不到的情况,在日志里面可以看出来,如下:</p>
<p>2012/04/24 13:55:35 2020#0: cache "cache_one" uses the "/var/logs/nginx/fastcgi_cache_dir" cache path while previously it used the "/var/logs/nginx/proxy_cache_dir" cache path<br />
2012/04/24 14:06:30 2020#0: cache "cache_one" uses the "/var/logs/nginx/fastcgi_cache_dir" cache path while previously it used the "/var/logs/nginx/proxy_cache_dir" cache path<br />
2012/04/24 14:16:03 2020#0: cache "cache_one" uses the "/var/logs/nginx/fastcgi_cache_dir" cache path while previously it used the "/var/logs/nginx/proxy_cache_dir" cache path<br />
2012/04/24 14:25:39 2020#0: cache "cache_fastcgi" uses the "/var/logs/nginx/proxy_cache_dir" cache path while previously it used the "/var/logs/nginx/fastcgi_cache_dir" cache path</p>
<p>这个问题让我纠结了好久,最后查看日志才发现没改缓存名的时候,调用的时候调用的是新路径,但是缓存的时候缓存到没改之前的路径里面去了。郁闷、</p>
<br />
<br />
原文地址:http://www.linuxyan.com/web-server/78.html
页:
[1]