Nginx FastCGI_cache only works for homepage


#1

I’ve installed nginx with cache purge module and enable it in /etc/nginx/sites-available/wordpress. Also installed nginx-help plugin, re-saved site permalinks.

Here’s the output of http://web-sniffer.net/ (homepage: HIT, other pages: BYPASS):

Connect to serverip on port 80 ... ok  

GET /index.php/2013/10/postname/ HTTP/1.1[CRLF]  
Host: domain[CRLF]  
Connection: close[CRLF]  
User-Agent: Web-sniffer/1.0.46 (+http://web-sniffer.net/)[CRLF]  
Accept-Charset: ISO-8859-1,UTF-8;q=0.7,*;q=0.7[CRLF]  
Cache-Control: no-cache[CRLF]  
Accept-Language: de,en;q=0.7,en-us;q=0.3[CRLF]  
Referer: http://web-sniffer.net/[CRLF]  

Status: HTTP/1.1 200 OK  
Server: nginx/1.4.1  
Date:   Fri, 01 Nov 2013 02:48:50 GMT  
Content-Type:   text/html; charset=UTF-8  
Transfer-Encoding:      chunked  
Connection:     close  
Vary:   Accept-Encoding  
X-Powered-By:   PHP/5.3.10-1ubuntu3.8  
X-Pingback:     http://domain/xmlrpc.php  
Link:   ; rel=shortlink  
rt-Fastcgi-Cache:       BYPASS

Content of /etc/nginx/sites-available/wordpress:

  
server {  
        listen   80; ## listen for ipv4; this line is default and implied  
        listen   [::]:80 default ipv6only=on; ## listen for ipv6  

        root /usr/share/nginx/www;  
        index index.php;  

        # Make site accessible from http://localhost/  
        server_name mydomainname;  

        set $no_cache 0;  

        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 / {  
                # First attempt to serve request as file, then  
                # as directory, then fall back to index.html  

                try_files $uri $uri/ /index.php?$args;  
                # Uncomment to enable naxsi on this location  
                # include /etc/nginx/naxsi.rules  
        }  

        location /doc/ {  
                alias /usr/share/doc/;  
                autoindex on;  
                allow 127.0.0.1;  
                deny all;  
        }  

       location ~* \.(jpg|jpeg|gif|png|css|js|ico|xml)$ {  
                 root /usr/share/nginx/www;  
                 access_log off;  
                 log_not_found off;  
                 expires 180d;  
                 valid_referers none blocked mydomain ~\.google\. ~\.yahoo\. ~\.bing\. ~\.facebook\. ~\.fbcdn\.;  
                 if ($invalid_referer) {  
                      return 403;  
                  }  
        }  

        #location /RequestDenied {  
                # For example, return an error code  
                #refturn 418;  
        #}  

        error_page 404 /404.html;  

        error_page 500 502 503 504 /50x.html;  
        location = /50x.html {  
                root /usr/share/nginx/www;  
        }  

        # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000  
        #  
        location ~ \.php$ {  
                #fastcgi_split_path_info ^(.+\.php)(/.+)$;  
                # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini  

                # With php5-cgi alone:  
                fastcgi_pass 127.0.0.1:9000;  
                # With php5-fpm:  
                #fastcgi_pass unix:/var/run/php5-fpm.sock;  
                #fastcgi_index index.php;  
                try_files $uri /index.php;  
                include fastcgi_params;  

                fastcgi_cache_bypass $no_cache;  
                fastcgi_no_cache $no_cache;  

                fastcgi_cache WORDPRESS;  
                fastcgi_cache_valid 60m;  
        }  

        # deny access to .htaccess files, if Apache's document root  
        # concurs with nginx's one  
        #  
        location ~ /purge(/.*) {  
            fastcgi_cache_purge WORDPRESS "$scheme$request_method$host$1";  
        }  

        location ~ /\.ht {  
                deny all;  
        }  
}  

#2