Jump to content

Nginx cache not refreshing after new content is posted


Recommended Posts

Hello there, I'm using nginx fastcgi caching with php-fpm with great success (in terms of performance), the only problem is that the cache is not purged automatically after a member create or update content.

For example: someone creates a new topic and no one can see it because nginx is serving a old cached version of the home.

If you try to enter in new topic directly, by the url, no problem. But anyone else can't see it until you purge everything.

Any ideas?

My virtual host conf: 

server {
    server_name XXX www.XXX;
    root /var/www/XXXX;

    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" a>

    index index.php;

    include /var/www/blog.redirects;
    include /var/www/forum.redirects;

    location /blog/ {
        try_files $uri $uri/ /blog/index.php?q=$uri&$args;
    }

    location /forum/ {
        try_files $uri $uri/ /forum/index.php;
    }
    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/*|/tag/.*/feed/*>
        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_cach>
        set $skip_cache 1;
    }
	
	location ~*  \.(jpg|jpeg|png|gif|ico|css|js|pdf)$ {
      expires 7d;
    }

    location ~ \.php$ {
       include snippets/fastcgi-php.conf;
       fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
       fastcgi_cache_bypass $skip_cache;
       fastcgi_no_cache $skip_cache;

       fastcgi_cache phpcache;
       fastcgi_cache_valid 200 301 302 60m;
       fastcgi_cache_use_stale error timeout updating invalid_header http_500 http_503;
       fastcgi_cache_min_uses 1;
       fastcgi_cache_lock on;
       add_header X-FastCGI-Cache $upstream_cache_status;
       add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload>
    }

I really don't know what else I could try.

Thank you for your time.

Link to comment
Share on other sites

12 hours ago, FabioPaz said:

Hello,

nginx 1.18.0 (Ubuntu 20.04 server)

php-fpm 7.4

Change this

location /forum/ {
        try_files $uri $uri/ /forum/index.php;
    }
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;
    }

to this

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

set $cache_uri $request_uri;

   # POST requests and URLs with a query string should always go to PHP
    if ($request_method = POST) {
        set $cache_uri 'null cache';
    }  
    if ($query_string != "") {
        set $cache_uri 'null cache';
    }  

 

Restart nginx and let me know.

Edited by Afrodude
Link to comment
Share on other sites

14 hours ago, FabioPaz said:

the only problem is that the cache is not purged automatically after a member create or update content.

The issue is that your configuration is caching all successful or redirect requests for 60 minutes.

fastcgi_cache_valid 200 301 302 60m;

Why did you set that value?

Documentation: http://nginx.org/en/docs/http/ngx_http_fastcgi_module.html#fastcgi_cache_valid

Link to comment
Share on other sites

8 hours ago, Afrodude said:

Change this


location /forum/ {
        try_files $uri $uri/ /forum/index.php;
    }
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;
    }

to this


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

set $cache_uri $request_uri;

   # POST requests and URLs with a query string should always go to PHP
    if ($request_method = POST) {
        set $cache_uri 'null cache';
    }  
    if ($query_string != "") {
        set $cache_uri 'null cache';
    }  

 

Restart nginx and let me know.

Thank you for your time, but the content is still outdated after editing a post (for example), it keeps the cached data.

I already tried to follow the Makoto's guide, but unfortunately I had several problems with differences in the config.

Since the topic is from 2015, maybe there's stuff that changed to the newer versions?

7 hours ago, Paul E. said:

The issue is that your configuration is caching all successful or redirect requests for 60 minutes.


fastcgi_cache_valid 200 301 302 60m;

Why did you set that value?

Documentation: http://nginx.org/en/docs/http/ngx_http_fastcgi_module.html#fastcgi_cache_valid

Hello, Paul.

If there's a topic without new changes, I want the cached version to be served as longer as possible. But if someone post in it or the content is changed, then I want the new version to be cached.

So caching for 60 minutes (or 120m for that matter), shouldn't be a problem if the cache is purged when a change happens, right?

Maybe this isn't the config that I think it is?

What is the best practice for fastcgi_cache_valid? Can you give me a hand?

To be honest, I read the doc, but simply didn't understand how to make it work.

Being a newbie in server management, nginx seems to be more complicated than "usual" (the same for their doc files).

Thank you for your time.

Edited by FabioPaz
Link to comment
Share on other sites

6 hours ago, FabioPaz said:

So caching for 60 minutes (or 120m for that matter), shouldn't be a problem if the cache is purged when a change happens, right?

Maybe this isn't the config that I think it is?

I don't think it does what you think it does. That config entry caches requests based on the response code for the period of time indicated. That may make sense on a site that changes infrequently, but not for an active forum.

What makes you think the cache is purged by any other means than the elapse of time? Nginx makes the request the first time, gets a 200 response, and then waits 60 minutes to ask again per your config, serving out the cached content until that time expires.

There would be no saving of overhead for Nginx to run php, but then not serve out updated results.

6 hours ago, FabioPaz said:

To be honest, I read the doc, but simply didn't understand how to make it work.

I'd encourage you to start with reading documentation on each available config option and implementing them one at a time to validate your understanding is correct.

Link to comment
Share on other sites

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...