AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 747646
Accepted
morleyc
morleyc
Asked: 2016-01-09 06:15:21 +0800 CST2016-01-09 06:15:21 +0800 CST 2016-01-09 06:15:21 +0800 CST

Nginx fastcgi_cache 不适用于一个 WP 站点(但对于 time.php 在不同的虚拟主机上工作正常)

  • 772

我正在使用 fpm php 5.5 运行 Ubuntu nginx 1.8,在一个站点上我能够使缓存正常工作(通过简单的时间输出)<?php echo time();?>显示:

Connection:keep-alive
Content-Encoding:gzip
Content-Type:text/html
Date:Fri, 08 Jan 2016 14:04:00 GMT
Fastcgi-Cache:HIT
Server:nginx/1.8.0
Transfer-Encoding:chunked
X-Powered-By:PHP/5.5.9-1ubuntu4.14

但是对于 WordPress 网站,标题始终显示(即使在注销和隐身时):

Cache-Control:no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Connection:keep-alive
Content-Encoding:gzip
Content-Type:text/html; charset=UTF-8
Date:Fri, 08 Jan 2016 14:02:42 GMT
Expires:Thu, 19 Nov 1981 08:52:00 GMT
Fastcgi-Cache:BYPASS
Link:<http://mywpsite.com/wp-json/>; rel="https://api.w.org/"
Link:<http://mywpsite.com/>; rel=shortlink
Pragma:no-cache
Server:nginx/1.8.0
Transfer-Encoding:chunked
Vary:Accept-Encoding
X-Powered-By:PHP/5.5.9-1ubuntu4.14

从 wiki 我尝试了不同的设置并压缩到以下单个文件(tester.com 有效,而 mywpsite.com 没有 - 这些是我在我的主机文件中设置的别名,并且肯定命中了正确的服务器)https://codex .wordpress.org/Nginx。

猫 /etc/nginx/nginx.conf

user www-data;
worker_processes 4;
pid /run/nginx.pid;

events {
        worker_connections 1024;
        # multi_accept on;
}

http {
        fastcgi_cache_key "$scheme$request_method$host$request_uri";

        sendfile on;
        tcp_nopush on;
        tcp_nodelay on;
        keepalive_timeout 65;
        types_hash_max_size 2048;

        include /etc/nginx/mime.types;
        default_type application/octet-stream;

        gzip on;
        gzip_disable "msie6";

        application/xml+rss text/javascript;

        # Upstream to abstract backend connection(s) for PHP.
        upstream php {
                #this should match value of "listen" directive in php-fpm pool
                server unix:/var/run/php5-fpm.sock;
        }

        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;

        add_header Fastcgi-Cache $upstream_cache_status;
}

猫 /etc/nginx/sites-available/tester.com

fastcgi_cache_path /home/tester.com/cache levels=1:2 keys_zone=MYAPP:100m inactive=60m;

server {
    listen   80;
    server_name tester.com;

    root /home/tester.com/public_html;
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ /index.html;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_cache MYAPP;
        fastcgi_cache_valid 200 60m;
    }
}

猫 /etc/nginx/sites-available/mywpsite.com

fastcgi_cache_path /home/mywpsite.com/cache levels=1:2 keys_zone=MYWPSITE:100m inactive=60m;

server {
        server_name mywpsite.com;
        root /home/mywpsite.com/public_html;

        index index.php;

        location = /favicon.ico {
                log_not_found off;
                access_log off;
        }

        location = /robots.txt {
                allow all;
                log_not_found off;
                access_log off;
        }

        location ~ /\. {
                access_log off;
                log_not_found off;
                deny all;
        }

        location ~* /(?:uploads|files)/.*\.php$ {
                deny all;
        }

        location / {
                try_files $uri $uri/ /index.php?$args;
        }

        # Add trailing slash to */wp-admin requests.
        rewrite /wp-admin$ $scheme://$host$uri/ permanent;

        set $skip_cache 0;

        if ($request_method = POST) {
            set $skip_cache 1;
        }

        if ($query_string != "") {
            set $skip_cache 1;
        }

        if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
            set $skip_cache 1;
        }

        if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
            set $skip_cache 1;
        }

        location ~* ^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
               access_log off; log_not_found off; expires max;
        }

        fastcgi_cache_bypass $skip_cache;
        fastcgi_no_cache $skip_cache;
        fastcgi_cache MYWPSITE;
        fastcgi_cache_valid 60m;

        location ~ [^/]\.php(/|$) {
                fastcgi_split_path_info ^(.+?\.php)(/.*)$;
                if (!-f $document_root$fastcgi_script_name) {
                        return 404;
                }
                # This is a robust solution for path info security issue and works with "cgi.fix_pathinfo = 1" in /etc/php.ini (default)

                include fastcgi.conf;
                fastcgi_index index.php;
                #       fastcgi_intercept_errors on;
                fastcgi_pass php;

                fastcgi_cache_bypass $skip_cache;
                fastcgi_no_cache $skip_cache;

                fastcgi_cache MYWPSITE;
                fastcgi_cache_valid 200 60m;
        }

        location ~ /purge(/.*) {
                fastcgi_cache_purge MYWPSITE"$scheme$request_method$host$1";
        }
}

道歉,因为我确信这是一些愚蠢的事情,但经过几个深夜后,我会很感激一些新鲜的眼睛来看待这个问题。

nginx
  • 1 1 个回答
  • 4676 Views

1 个回答

  • Voted
  1. Best Answer
    Tim
    2016-01-09T11:28:17+08:002016-01-09T11:28:17+08:00

    您以一种奇怪的方式在配置文件之间进行拆分。它对 nginx 没有任何影响,但它使人们阅读起来更加困难。

    这是我将在某个时候发布的教程的合理记录工作配置 - 我怀疑 fastcgi_ignore_headers 可能会有所帮助。我在单个站点文件的顶部有这个,但它可以进入 nginx.conf。检查您的文件权限,我不认为是这样,但这对于任何清除缓存的进程都非常重要 - 这部分非常棘手。

    # Caching. Putting the cache into /dev/shm keeps it in RAM, limited to 10MB, for one day. Or put it into a more standard /etc/nginx/cache
    # You can move to disk if you like, or extend the caching time
    fastcgi_cache_path /etc/nginx/cache levels=1:2 keys_zone=WORDPRESS:10m inactive=1440m; #Disk
    #fastcgi_cache_path /dev/shm/nginxcache levels=1:2 keys_zone=WORDPRESS:10m inactive=1440m; #RAM
    
    # This is to do with how the files are laid out on disk
    fastcgi_cache_key "$scheme$request_method$host$request_uri";
    
    # Determines in which cases a stale cached response can be used when an error occurs during communication with the FastCGI server
    fastcgi_cache_use_stale error timeout invalid_header http_500;
    
    # Wordpress themes (especially Photocrati) often send back inappropriate headers, so ignore them
    fastcgi_ignore_headers Cache-Control Expires Set-Cookie ;
    
    # Rules to work out when cache should/shouldn't be used
    set $skip_cache 0;
    # POST requests and urls with a query string should always go to PHP
    if ($request_method = POST) {
            set $skip_cache 1;
    }
    if ($query_string != "") {
        set $skip_cache 1;
    }
    # Don't cache uris containing the following segments
    if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
        set $skip_cache 1;
    }
    # Don't use the cache for logged in users or recent commenters
    if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
        set $skip_cache 1;
    }
    

    这是我的位置块。请注意,我使用的是 Facebook 编写的 HHVM PHP 解释器,它比 PHP5 更快更高效,而 PHP7 不适用于我的 Wordpress 主题 Photocrati

      # Send HipHop and PHP requests to HHVM
      location ~ \.(hh|php)$ {
      fastcgi_keep_conn on;
      fastcgi_intercept_errors on;
      fastcgi_pass   php;
      include            fastcgi_params;
      fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
    
      # Use the cache defined above. Cache only 200 (success) status's, for 24 hours
      # Only cache GET and HEAD requests
      fastcgi_cache WORDPRESS;
      fastcgi_cache_valid 200 1440m;
      add_header X-Cache $upstream_cache_status;
    
      fastcgi_cache_methods GET HEAD;
      fastcgi_cache_bypass $skip_cache;
      fastcgi_no_cache $skip_cache;
    
      # Clear the server ID, for security. Clear the cache control headers. 
      more_clear_headers Server; more_clear_headers "Pragma";
    
      # Add cache control headers that say each page is valid for an hour
      add_header Z_LOCATION "PHP MAIN"; add_header URI $uri; # DEBUG
      }
    

    然后在每个位置块内放置以下内容以帮助调试正在发生的事情 - 它会告诉您变量和正在使用的位置块,这将帮助您调试。使用 Firefox 和插件“Live HTTP Headers”查看输出

    add_header Z_LOCATION "(name of your location block)"; # DEBUG
    add_header Z_URI $uri; # DEBUG
    add_header Z_CACHE $skip_cache; # DEBUG
    add_header URI $uri; # DEBUG
    
    • 5

相关问题

  • Gzip 与反向代理缓存

  • nginx 作为代理的行为

  • Nginx 学习资源 [关闭]

  • 提供 70,000 个静态文件 (jpg) 的最佳方式?

  • 在 Apache、LightTPD 和 Nginx Web 服务器上提供 PHP 5.x 应用程序的现状?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve