找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 96|回复: 1

[L/WNAMP] nginx有两种缓存机制:fastcgi_cache和proxy_cache

[复制链接]
发表于 2024-2-10 16:01 | 显示全部楼层 |阅读模式
nginx有两种缓存机制:fastcgi_cache和proxy_cache
下面我们来说说这两种缓存机制的区别吧
proxy_cache作用是缓存后端服务器的内容,可能是任何内容,包括静态的和动态的
fastcgi_cache作用是缓存fastcgi生成的内容,很多情况是php生成的动态内容
proxy_cache缓存减少了nginx与后端通信的次数,节省了传输时间和后端带宽
fastcgi_cache缓存减少了nginx与php的通信次数,更减轻了php和数据库的压力。
 
proxy_cache缓存设置
#注:proxy_temp_path和proxy_cache_path指定的路径必须在同一分区
proxy_temp_path   /data0/proxy_temp_dir;
#设置Web缓存区名称为cache_one,内存缓存空间大小为200MB,1天没有被访问的内容自动清除,硬盘缓存空间大小为30GB。
proxy_cache_path  /data0/proxy_cache_dir  levels=1:2   keys_zone=cache_one:200m inactive=1d max_size=30g;
server
  {
    listen       80;
    server_name  www.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_valid  200 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_log  off;
  }
}
 
fastcgi_cache缓存设置
#定义缓存存放的文件夹
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 200 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,经查需要定义这句话
        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;
}
总的来说  nginx的proxy_cache和fastcgi_cache的缓存配置差不多。
 楼主| 发表于 2024-2-10 16:27 | 显示全部楼层

nginx不仅有个大家很熟悉的缓存代理后端内容的proxy_cache,还有个被很多人忽视的fastcgi_cache。
proxy_cache的作用是缓存后端服务器的内容,可能是任何内容,包括静态的和动态。
fastcgi_cache的作用是缓存fastcgi生成的内容,很多情况是php生成的动态的内容。
proxy_cache缓存减少了nginx与后端通信的次数,节省了传输时间和后端宽带。
fastcgi_cache缓存减少了nginx与php的通信的次数,更减轻了php和数据库(mysql)的压力,这比用memcached之类的缓存要轻松得多。
但是,缓存也有弊端,比如说评论了之后不能会当时显示等等,自己取舍,有得必有失。

本着测试的原则,在本站上测试了一下fastcgi_cache这个功能,,貌似还不错,减少数据库的查询了,顺便优化下fastcgi的参数,的嘿嘿,,贴上配置nginx的httpd段里面加入

fastcgi_connect_timeout 300;
指定连接到后端FastCGI的超时时间。
fastcgi_send_timeout 300;
向FastCGI传送请求的超时时间,这个值是指已经完成两次握手后向FastCGI传送请求的超时时间。
fastcgi_read_timeout 300;
接收FastCGI应答的超时时间,这个值是指已经完成两次握手后接收FastCGI应答的超时时间。
fastcgi_buffer_size 32k;
指定读取FastCGI应答第一部分需要用多大的缓冲区,一般第一部分应答不会超过1k,由于页面大小为4k,所以这里设置为4k。
fastcgi_buffers 4 32k;
定本地需要用多少和多大的缓冲区来缓冲FastCGI的应答请求。如果一个PHP脚本所产生的页面大小为256KB,那么会为其分配4个64KB的缓冲区来缓存;如果页面大小大于256KB,那么大于256KB的部分会缓存到fastcgi_temp指定的路径中,但是这并不是好方法,因为内存中的数据处理速度要快于硬盘。一般这个值应该为站点中PHP脚本所产生的页面大小的中间值,如果站点大部分脚本所产生的页面大小为256KB,那么可以把这个值设置为“16 16k”、“4 64k”等。
fastcgi_busy_buffers_size 64k;
默认值是fastcgi_buffers的两倍。
fastcgi_temp_file_write_size 64k;
在写入fastcgi_temp_path时将用多大的数据块,默认值是fastcgi_buffers的两倍。
fastcgi_cache_path /var/logs/nginx/fastcgi_cache_dir levels=1:2 keys_zone=cache_fastcgi:128m inactive=1d max_size=10g;
这个指令为FastCGI缓存指定一个路径,目录结构等级,关键字区域存储时间和非活动删除时间。以及最大占用空间。

然后在server的location里面调用,如下缓存php文件:

location ~ \.php$ {
    root /var/www/html/yan_blog;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
    fastcgi_cache cache_fastcgi;
    表示开启FastCGI缓存并为其指定一个名称。
    fastcgi_cache_valid 200 302 301 1h;
    fastcgi_cache_valid any 1m;
    为指定的应答代码指定缓存时间,如上例中将200,302 301应答缓存一小时,其他为1分钟。
    fastcgi_cache_min_uses 1;
    设置链接请求几次就被缓存。
    fastcgi_cache_use_stale error timeout invalid_header http_500;
    定义哪些情况下用过期缓存
    fastcgi_cache_key $request_method://$host$request_uri;
    注意一定要加上$request_method作为cache key,否则如果HEAD类型的先请求会导致后面的GET请求返回为空
}

设置了之后重启nginx就可以生效了,这个时候再访问php的页面的话,就会被缓存了,可以查看/var/logs/nginx/fastcgi_cache_dir这个目录下面是有缓存文件的。最后再说明一点,如果更改了缓存目录的路径,一定要把缓存的名称也改掉,后端调用的名称也同步改掉,如果只改掉了缓存目录,不改缓存名称的话,缓存的时候还是会缓存到之前的路径下面去,但是调用的时候调用的是新的路径,这个时候就会出现找不到的情况,在日志里面可以看出来,如下:

2012/04/24 13:55:35 [emerg] 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
2012/04/24 14:06:30 [emerg] 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
2012/04/24 14:16:03 [emerg] 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
2012/04/24 14:25:39 [emerg] 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

这个问题让我纠结了好久,最后查看日志才发现没改缓存名的时候,调用的时候调用的是新路径,但是缓存的时候缓存到没改之前的路径里面去了。郁闷、



原文地址:http://www.linuxyan.com/web-server/78.html
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|学习笔记

GMT+8, 2024-5-3 15:01 , Processed in 0.038170 second(s), 14 queries , APCu On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表