Configuring Nginx Cache in WordPress Separately for Blog and New Posts to Reflect New Content

4 min

language: ja bn en es hi pt ru zh-cn zh-tw

Hello, I'm incompetent.

This site builds and caches a similar mechanism on its own without using Cloudflare.

This way, I can manage everything myself, including all the 'itchy spots', and it's simply more fun to do everything yourself!

To apply the following settings, I decided to insert /blog/ into the permalinks of this blog's posts from the WordPress settings.
Please note that this syntax also includes reverse proxy settings, so be sure to carefully choose what is necessary and what is not.

Nginx Cache Settings



            location / {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_cache_bypass $bypass $do_not_cache;
        proxy_no_cache $do_not_cache;
        proxy_redirect off;
        proxy_cache_revalidate on; 
        proxy_ignore_headers Cache-Control Expires;
        proxy_hide_header Cache-Control;
        proxy_hide_header Pragma;
        add_header Content-Security-Policy upgrade-insecure-requests;
        add_header X-Cache-Status $upstream_cache_status;

        proxy_cache             your_cache1;

        proxy_pass http://192.168.10.101/; 
    }
        location /blog/ {
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_cache_bypass $bypass $do_not_cache;
        proxy_cache_bypass $http_cache_control $http_if_modified_since;
        
        proxy_no_cache $do_not_cache;
        proxy_redirect off;
        proxy_cache_revalidate on; 
        proxy_ignore_headers Cache-Control Expires;
        proxy_hide_header Cache-Control;
        proxy_hide_header Pragma;
        add_header Content-Security-Policy upgrade-insecure-requests;
        add_header X-Cache-Status $upstream_cache_status;

        proxy_cache             your_cache2;

        proxy_pass http://192.168.10.101/blog/; 

    

By changing the `proxy_cache` values, I can change the cache location, and by deleting `your_cache1` while keeping the blog's cache, it became possible to reflect new posts while retaining the /blog cache.

However, in reality, wouldn't it be better to set `your_cache1` for a short period with `proxy_cache_valid` and keep `your_cache2` for /blog/ for a longer period?
I have this thought as well, so I plan to adjust this over time.

Related Posts