php - nginx不可以缓存伪静态网页吗?

 为谁落慕 发布于 2022-12-01 16:42

最近想用nginx的proxy_cache缓存网站的一些页面,一台nginx做proxy,两台web,web站是PHP的伪静态页面,测试的时候发现nginx无法缓存后端为伪静态的html,静态的html可以!是我哪里配置错了,还是nginx本身就无法缓存伪静态页面?

6 个回答
  • 如果后端有set-cookie,nginx也是不会缓存的,加上此配置proxy_ignore_headers Set-Cookie就可缓存。另外这几个头部(Expires、E-Tags、Last-Modified、Cache-Control)的值对缓存也是影响的

    2022-12-01 17:34 回答
  • 如果返回的页面没有返回 Expires、E-Tags、Last-Modified、Cache-Control 等表示这个文件可以缓存的http头,nginx是不会进行缓存的
    这跟浏览器的行为是一样的, 如果server返回的http头部表明了这个文件可以缓存,nginx或者浏览器才会进行缓存,否则就认为是动态页面。

    2022-12-01 17:34 回答
  • 这个时候nginx-cache-purge这个模块就很有用了,他可以帮助你管理和清除fastcgi_cache

    安装方法

    mkdir /home/cache/wpcache -p
    cd ~
    ./lnmp stop
    apt-get install git -y #centos用yum install git -y
    git clone https://github.com/FRiCKLE/ngx_cache_purge
    wget http://soft.vpser.net/web/nginx/nginx-1.0.15.tar.gz
    tar zxvf nginx-1.0.15.tar.gz
    cd nginx-1.0.15/
    ./configure --user=www --group=www --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-ipv6 --add-module=../ngx_cache_purge
    make
    make install
    /root/lnmp start
    

    这里我以wordpress为例,发一个配置方法

    http

    #在http层添加以下三行代码
    fastcgi_cache_path /home/cache/wpcache levels=1:2 keys_zone=wpcache:10m inactive=20m;
    fastcgi_cache_key "$scheme$request_method$host$request_uri";
    fastcgi_cache_use_stale error timeout invalid_header http_500;
    
    #wpcache为名字,可以随便修改。10m为内存占用
    

    server

        set $no_cache 0;
    
        # 不缓存POST操作
        if ($request_method = POST) {
            set $no_cache 1;
        }   
        if ($query_string != "") {
            set $no_cache 1;
        }   
    
        # 不缓存后台
        if ($request_uri ~* "(/wp-admin/|/xmlrpc.php|/wp-(app|cron|login|register|mail).php|wp-.*.php|/feed/|index.php|wp-comments-popup.php|wp-links-opml.php|wp-locations.php|sitemap(_index)?.xml|[a-z0-9_-]+-sitemap([0-9]+)?.xml)") {
            set $no_cache 1;
        }   
    
        # 已登录的不缓存(防止留言串号)
        if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
            set $no_cache 1;
        }
    
        location / {
            try_files $uri $uri/ /index.php?$args;
        }    
    
        location ~ .php$ {
            try_files $uri /index.php; 
            include fastcgi_params;        #Lnmp.org一键包改成include fcgi.conf;
            fastcgi_pass unix:/var/run/php5-fpm.sock;     #Lnmp.org一键包改成fastcgi_pass  unix:/tmp/php-cgi.sock;
    
            fastcgi_cache_bypass $no_cache;
                fastcgi_no_cache $no_cache;
    
            fastcgi_cache wpcache;    #要跟前面设置的名称一样
            fastcgi_cache_valid  30m;   #缓存时间
        }
    
        location ~ /purge(/.*) {
                allow 11.22.33.44;  #此处该为你vps的ip
                allow 8.8.8.8;
                allow 127.0.0.1;
                deny all;
            fastcgi_cache_purge wpcache "$scheme$request_method$host$1";
        }
    
    2022-12-01 17:34 回答
  • 估计你了解php-fpm中的缓存。

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

    via http://www.linuxyan.com/web-server/78.html

    2022-12-01 17:34 回答
  • 2022-12-01 17:34 回答
  • 本来想谈谈fastcgi_cache,但是 @李惟 长篇大论了一番,抢了我的彩头。
    如果那些伪静态页面不常改动,在HTTP Header里也配置上缓存,连nginx的事都省了不少。

    2022-12-01 17:34 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有