C4f1151075b447779af31e99d6cf70e2c6eb47ac
By:
newbie

FastCGI issue on wordpress multisite

March 28, 2017 1k views
WordPress LEMP Caching Ubuntu 16.04

Hi,
i have FastCGI enabled and its working perfectly on my main site. but for my other sites which is in sub directory multisite setup, FastCGI is caching even the admin area too.
i dont understand why FasctCGI is not bypassing sub-directory sites admin area when cache bypass is working for main sites admin area.
i have opcache enabled as well. is that might be any issue here?

here is the response header from post page

Request URL:http://mysite.com/site2/wp-admin/edit.php
Request Method:GET
Status Code:200 OK
Remote Address:139.162.171.19:80
Referrer Policy:no-referrer-when-downgrade

Cache-Control:no-cache, must-revalidate, max-age=0
Connection:keep-alive
Content-Encoding:gzip
Content-Type:text/html; charset=UTF-8
Date:Tue, 28 Mar 2017 16:17:56 GMT
Expires:Wed, 11 Jan 1984 05:00:00 GMT
Server:nginx/1.10.3
Transfer-Encoding:chunked
Vary:Accept-Encoding
X-Cache:HIT
X-Frame-Options:SAMEORIGIN

and these are my FastCGI config

fastcgi_cache_path /var/run/cache levels=1:2 keys_zone=MYAPP:100m inactive=60m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
add_header X-Cache $upstream_cache_status;
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
        #Cache everything by default
        set $no_cache 0;

        #Don't cache POST requests
        if ($request_method = POST) {
                set $no_cache 1;
        }

        #Don't cache if the URL contains a query string
        if ($query_string != "") {
                set $no_cache 1;
        }

        #Don't cache the following URLs
        if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
                set $no_cache 1;
        }

        #Don't cache if there is a cookie called PHPSESSID
        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$is_args$args;
                # try_files $uri $uri/ /index.html;
                # try_files $uri $uri/ =404;
        }
        location ~ \.php$ {
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/run/php/php7.0-fpm.sock;
                include fastcgi_params;
                fastcgi_param REQUEST_METHOD $request_method;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                fastcgi_cache MYAPP;
                fastcgi_cache_valid 200 60m;
                fastcgi_cache_bypass $no_cache;
                fastcgi_no_cache $no_cache;
        }

and this is what i have for enabling multi-site

        if (!-e $request_filename) {
                rewrite /wp-admin$ $scheme://$host$uri/ permanent;
                rewrite ^(/[^/]+)?(/wp-.*) $2 last;
                rewrite ^(/[^/]+)?(/.*\.php) $2 last;
        }
1 Answer

@newbie

As noted in the other thread, please post the full server block, top to bottom. Feel free to change the domain if you'd like, though make sure everything else is verbatim what you're using.

  • @jtittle
    heres my entire server block

    fastcgi_cache_path /var/run/cache levels=1:2 keys_zone=MYAPP:100m inactive=60m;
    fastcgi_cache_key "$scheme$request_method$host$request_uri";
    add_header X-Cache $upstream_cache_status;
    fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
    
        # Expires map
        map $sent_http_content_type $expires {
        default         off;
        text/html       epoch;
        text/css        30d;
        application/javascript  30d;
        ~image/         max;
        font/woff2      max;
        font/woff       max;
        image/x-icon        max;
        application/font-woff2  max;
    }
    
    server {
        listen 80 default_server;
        listen [::]:80 default_server;
    
        expires $expires;
    
        server_name mydomain.com www.mydomain.com;
    
        # SSL configuration
        #
        # listen 443 ssl default_server;
        # listen [::]:443 ssl default_server;
    
        # include snippets/snakeoil.conf;
    
        root /var/www/html/website;
    
        # Add index.php to the list if you are using PHP
        index index.php index.html;
    
        if (!-e $request_filename) {
            rewrite /wp-admin$ $scheme://$host$uri/ permanent;  
            rewrite ^(/[^/]+)?(/wp-.*) $2 last;                     
            rewrite ^(/[^/]+)?(/.*\.php) $2 last;                   
        }
    
        #Cache everything by default
        set $no_cache 0;
    
        #Don't cache POST requests
        if ($request_method = POST) {
            set $no_cache 1;
        }
    
        #Don't cache if the URL contains a query string
        if ($query_string != "") {
            set $no_cache 1;
        }
    
        #Don't cache the following URLs
        if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
            set $no_cache 1;
        }
    
        #Don't cache if there is a cookie called PHPSESSID
        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$is_args$args;
            # try_files $uri $uri/ /index.html;
            # try_files $uri $uri/ =404;
        }
    
        location ~ \.php$ {
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            include snippets/fastcgi-php.conf;
            fastcgi_pass unix:/run/php/php7.0-fpm.sock;
            include fastcgi_params;
            fastcgi_param REQUEST_METHOD $request_method;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_cache MYAPP;
            fastcgi_cache_valid 200 60m;
            fastcgi_cache_bypass $no_cache;
            fastcgi_no_cache $no_cache;
        }
    
        location ~ /\.ht {
            deny all;
        }
    
        location /xmlrpc.php {
            deny all;
        }
    
        location = /favicon.ico { log_not_found off; access_log off; }
        location = /robots.txt { log_not_found off; access_log off; allow all; }
    }
    

    in my php.ini

    cgi.fix_pathinfo = 0
    

    i have noticed few other things from https://codex.wordpress.org/Nginx
    i installed multi-site and FastCGI according DO tutorials and few things from above link is missing in those tutorials. just want to confirm they are not the reason im facing this problem.

    map $uri $blogname{
        ~^(?P<blogpath>/[^/]+/)files/(.*)       $blogpath ;
    }
    

    thanks in advance

    • @jtittle
      i must have done something wrong. i just also noticed i dont have any folder "blogs.dir" inside my wp-content.
      :( :(
      what should i do now?

      • @newbie

        Not sure why, but I didn't get an alert on this one, even though you tagged me.

        WARNING - This is a long post :-).

        The issue with FastCGI Cache is in how it works -- it can be a major performance gain, while also being a major pain if you don't know how it works.

        The first thing to note is that FastCGI Cache caches the output of the page you see as a fully rendered page. If you were to visit a website, view the source, copy and paste that to a binary file, and then hit that page instead of a dynamically generated page, that's essentially FastCGI Caching (in very basic terms).

        Why this matters is because of what it does. FastCGI Cache essentially strips away the dynamic aspect of your site when a cache hit is true and renders it as a static page.

        Once this happens, until you clear the FastCGI Cache, requests for anything that has been previously cached will still be served. So if you made a mistake in your config, but FastCGI Cache was enabled when that mistake was made, the cache still runs and any request for a page still gets cached if it doesn't exist and isn't expired.

        Without the fastcgi_cache_purge module, purging the cache has to be done by hand from the CLI. The issue here is that, from what I've read on their GitHub issues page, it doesn't work with the latest versions of NGINX. I've not tested this to confirm, though it's not the first time I've read this so I'm leaning towards it being true until I can test it on my own private Droplet. So until I can confirm, you'd need to test it or rely on clearing it manually.

        So to clear the cache, if you're using /var/run/cache, you'd run:

        rm -rf /var/run/cache/*
        

        Which will delete everything in ./cache, but not ./cache itself.

        ...

        What I Recommend

        First, comment out the FastCGI setup. Until you get everything else working, having it active is going to cause issues.

        Your configuration will end up looking like this:

        #fastcgi_cache_path /var/run/cache levels=1:2 keys_zone=MYAPP:100m inactive=60m;
        #fastcgi_cache_key "$scheme$request_method$host$request_uri";
        #add_header X-Cache $upstream_cache_status;
        #fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
        
        # Expires map
        map $sent_http_content_type $expires {
            default                 off;
            text/html               epoch;
            text/css                30d;
            application/javascript  30d;
            ~image/                 max;
            font/woff2              max;
            font/woff               max;
            image/x-icon            max;
            application/font-woff2  max;
        }
        
        server {
            listen 80 default_server;
            listen [::]:80 default_server;
        
            expires $expires;
        
            server_name mydomain.com www.mydomain.com;
        
            # SSL configuration
            #
            # listen 443 ssl default_server;
            # listen [::]:443 ssl default_server;
        
            # include snippets/snakeoil.conf;
        
            root /var/www/html/website;
        
            # Add index.php to the list if you are using PHP
            index index.php index.html;
        
            if (!-e $request_filename) {
                rewrite /wp-admin$ $scheme://$host$uri/ permanent;  
                rewrite ^(/[^/]+)?(/wp-.*) $2 last;                     
                rewrite ^(/[^/]+)?(/.*\.php) $2 last;                   
            }
        
            #Cache everything by default
            #set $no_cache 0;
        
            #Don't cache POST requests
            #if ($request_method = POST) {
            #    set $no_cache 1;
            #}
        
            #Don't cache if the URL contains a query string
            #if ($query_string != "") {
            #    set $no_cache 1;
            #}
        
            #Don't cache the following URLs
            #if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") {
            #    set $no_cache 1;
            #}
        
            #Don't cache if there is a cookie called PHPSESSID
            #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$is_args$args;
            }
        
            location ~ \.php$ {
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/run/php/php7.0-fpm.sock;
                include fastcgi_params;
                fastcgi_param REQUEST_METHOD $request_method;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                #fastcgi_cache MYAPP;
                #fastcgi_cache_valid 200 60m;
                #fastcgi_cache_bypass $no_cache;
                #fastcgi_no_cache $no_cache;
            }
        
            location ~ /\.ht {
                deny all;
            }
        
            location /xmlrpc.php {
                deny all;
            }
        
            location = /favicon.ico { log_not_found off; access_log off; }
            location = /robots.txt { log_not_found off; access_log off; allow all; }
        }
        

        The next thing we need to look at is this:

                fastcgi_split_path_info ^(.+\.php)(/.+)$;
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/run/php/php7.0-fpm.sock;
                include fastcgi_params;
                fastcgi_param REQUEST_METHOD $request_method;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        

        I'd need to see:

        snippets/fastcgi-php.conf
        

        and

        fastcgi_params
        

        If either of this have redundant configuration, we need to fix that as with either of those, you shouldn't need:

                fastcgi_param REQUEST_METHOD $request_method;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        

        Those should already exist in one of those files, but you most likely don't need both, so let's see what's inside each of those files (please note which file is which in your reply).

        • @newbie

          Quick Update -- The NGINX module doesn't seem to be working from my tests, at least not on the latest Mainline version of NGINX (1.11.12).

          That makes purging without doing it manually a bit harder than it should be, short of setting up the normal (core) fastcgi purger included in NGINX. With that, though, it would require that you're able to send a PURGE request which isn't something that WordPress natively does.

          There are other configuration options that can extend fastcgi_cache, though for the time being, and until we get everything else working, I think it'd be best to comment out the settings (as I've shown above) and work from there.

          • @jtittle
            i really really appreciate your continuous co-operation buddy. and i must say im really amused with your support.

            i already tried deleting entire cache folder and then i checked my second sites wp-admin area, firs time it says miss but for the second time its a HIT. where my first sites admin area is always bypass.
            so i think i did something wrong on setting up wordpress multi-site. as its still isn't on production, i would like to set it up once again just to learn little deeply this time.

            thank you many times for all the testing u went through for me. cache u with updates shortly.

Have another answer? Share your knowledge.