web-dev-qa-db-ja.com

Nginxfastcgi_cacheが1つのWPサイトで機能しない(ただし、別の仮想ホストでtime.phpに対しては問題なく機能する)

私はUbuntunginx1.8をfpmphp 5.5で実行していますが、あるサイトでキャッシュを機能させることができます(単純な時間出力で)<?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

ウィキから私はさまざまな設定を試し、次の個々のファイルに圧縮しました(tester.comは機能しますが、mywpsite.comは機能しません-これらはホストファイルに設定したエイリアスであり、間違いなく適切なサーバーにアクセスします) https ://codex.wordpress.org/Nginx

cat /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;
}

cat /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;
    }
}

cat /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";
        }
}

お詫び申し上げますが、これはばかげたことだと思いますが、深夜に何度も行った後は、新鮮な目でこれを確認していただければ幸いです。

1
g18c

奇妙な方法で設定ファイル間で物事が分割されています。 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とプラグイン「LiveHTTPHeaders」を使用して出力を確認します

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
3
Tim