WordPress.org

Ready to get started?Download WordPress

Forums

Nginx
[resolved] Page Caching 404 (7 posts)

  1. Dmivlagus
    Member
    Posted 9 months ago #

    Hello! Please tell me how to turn the page cache 404. I use Nginx-Helper and fastCGI cache. Everything works fine, but the 404 pages create are a large load on the server. I would like to enable the cache for 404 pages.
    P.S. Sorry, my English is bad

    http://wordpress.org/plugins/nginx-helper/

  2. rahul286
    Member
    Plugin Author

    Posted 9 months ago #

    @dmivlagus

    Nginx helper doen't cache anything. Caching is handled on nginx side.

    You can use an nginx config like - http://rtcamp.com/wordpress-nginx/tutorials/single-site/fastcgi-cache-with-purging/

    In nginx config like above:
    Replace line:

    fastcgi_cache_valid 60m;

    with

    fastcgi_cache_valid 200 301 202 404 60m;

    Above will cache 404 pages on nginx's end.

  3. Dmivlagus
    Member
    Posted 9 months ago #

    @rahul286

    Unfortunately, it does not work. If a lot of time to refresh the page 404, php-fpm create are a large load on the server. Screenshoot
    My nginx.conf

    server {
    	listen   80;
    	server_name example.com www.example.com;
    
    	root /home/server/htdocs/example.com/www;
    
    	access_log /home/server/logs/example.com/nginx.access.log;
    	error_log /home/server/logs/example.com/nginx.error.log;
    	index index.php index.html index.htm;
    
    	location ~ \wp-login.php {
    	auth_basic "Unauthorized";
    	auth_basic_user_file /home/server/.htpasswd;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            include fastcgi_params;
            }
    
    	#Block HotLink
    	location ~ \.(jpeg|png|gif|jpg)$ {
    		valid_referers none blocked example.com *.example.com;
    		if ($invalid_referer) {
    		return 403;
    		}
    	}	
    
            #cache static files
       	location ~ ^/(images|javascript|js|css|flash|media|static)/ {
         		root /home/server/htdocs/example.com/www;
          		expires 10d;
        	}
    
    	error_page 404 /404.html;
    	error_page 500 502 503 504 /50x.html;
    	location = /50x.html {
    		root /etc/nginx/error;
    	}
    
    	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-(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 $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;
    	}
    
    	location ~ .php$ {
    		try_files $uri /index.php;
    		include fastcgi_params;
    		fastcgi_pass unix:/var/run/php5-fpm.sock;
    		fastcgi_cache_bypass $skip_cache;
    	        fastcgi_no_cache $skip_cache;
    
    		fastcgi_cache WORDPRESS;
    		fastcgi_cache_valid  60m;
    	}
    
    	location ~ /purge(/.*) {
    	    fastcgi_cache_purge WORDPRESS "$scheme$request_method$host$1";
    	}	
    
    	location / {
    	try_files $uri $uri/ /index.php?$args;
    	}
    	location = /favicon.ico {
    		log_not_found off;
    		access_log off;
    	}
    	location = /robots.txt {
    		allow all;
    		log_not_found off;
    		access_log off;
    	}
    	location ~ /\. {
    		deny all;
    	}
    	location ~* /(?:uploads|files)/.*\.php$ {
    		deny all;
    	}
    
    	rewrite /wp-admin$ $scheme://$host$uri/ permanent;
    
    	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;
    	}
    }

    I checked server headers. They are correct.
    Please tell me what could be the reason?

  4. rahul286
    Member
    Plugin Author

    Posted 9 months ago #

    Change:

    location ~ .php$ {
    		try_files $uri /index.php;

    To:

    location ~ .php$ {
    		try_files $uri =404;

    Let me know if it works for you?

  5. Dmivlagus
    Member
    Posted 9 months ago #

    I changed this section and did so:

    location ~ .php$ {
      try_files $uri =404;
      include fastcgi_params;
      fastcgi_pass unix:/var/run/php5-fpm.sock;
      fastcgi_cache_bypass $skip_cache;
      fastcgi_no_cache $skip_cache;
      fastcgi_cache WORDPRESS;
      fastcgi_cache_valid 200 301 202 404 60m;
    }

    It does not work.
    When I look at the source code of the page. I see the cached information that displays Nginx-Helper bottom of the page. For example,

    <!--Cached using Nginx-Helper on 2013-10-25 17:19:51. It took 14 queries executed in 0,587 seconds.-->
    <!--Visit http://wordpress.org/extend/plugins/nginx-helper/faq/ for more details-->

    If I refresh the page, the information at the bottom of the page changes.
    For example,
    Cached using Nginx-Helper on 2013-10-25 17:29:54
    Cached using Nginx-Helper on 2013-10-25 17:29:59.
    Cached using Nginx-Helper on 2013-10-25 17:30:04.
    Perhaps the reason for this?

    Thank you for helping me

  6. rahul286
    Member
    Plugin Author

    Posted 9 months ago #

    If this is happening for all pages, try adding following line:

    fastcgi_ignore_headers Cache-Control Expires Set-Cookie;

    Above:

    fastcgi_cache_valid 200 301 202 404 60m;

  7. Dmivlagus
    Member
    Posted 9 months ago #

    It's working! Thank you so much! :)

Reply

You must log in to post.

About this Plugin

About this Topic