Cache purging ends in "502 Bad Gateway"

The 502 gateway is becouse php request terminates(request_terminate_timeout = 30s in php-fpm)

The wordpress instance have 19K posts, i think this is the real problem.

setup: nginx + ngx_cache_purge + nginx-helper + php-fpm + apc

any advice?

You can change timeouts value by following –

yeah, but incrementing the timeout it's just a workaround, and degradates a lot the user experience, having to wait for more than 30 seconds to save a new wordpress post.

i'm looking for a more optimal way to invalidate nginx fastcgi cache.

PHP should come into picture for backend (wp-admin) only.

Frontend users, specially those who are not logged in should see cached content as much as possible.

We need to increase PHP timeout for some plugins which has long running processes.

Now, talking about user-experience, first thing you can do is use page-cache. Make sure your caching method passes tests mentioned here.

Next, use CSS & JS combining and minification. This will reduce number of network requests.

Also add CDN for static content e.g. images, css, js files. They will further improve performance.

Use a service like - It will guide you better way. If you get "A" grade in Y-Slow & pagespeed and still feels your site is slow then you may need to buy more hardware RAM/CPU/Wider port. You can try (we use them. Best value for money.)

When i say user experience, i mean the wordpress administrators, not the frontend users, caching is working great, purging it every time someone create a post in the blog site, is when the timeouts starts.

Looking at the nginx-helper source, i can see it happend when "purge_them_all" iterates over every post just to drop caches.

Ah! Sorry for confusion.

I am aware of performance-issue and sub-quality of faux-purge-all feature.

A workaround is already there - and will be added to nginx-helper soon. As nginx-helper is not our commercial project, it takes some time to fix issues.

For now you can copy code-snippet from and put it in a file say clean.php in webroot. Then you just need to hit from your browser to get entire cache flushed.

IMPORTANT: Make sure, unlinkRecursive("/var/run/nginx-cache/", false) ; has correct cache path. If you give that function a wrong path, it will delete content from there recurrseively.

Security concern is a big reason for taking so much time to add such a small piece of code in nginx-helper plugin. A small mistake and some data will could be lost forever!

Please let me know if this code-snippet work for you. You are also free to patch nginx-helper and send pull request at :-)

Yeah, this should work as a workaround, but is very dependent of the setup, because php could run with other user/group or even in other machine.

Maybe the correct solution is adding support in ngx_cache_purge plugin to delete the whole cache.

Ideally this should be handled by ngx_cache_purge module only. It will me more efficient and safe.

Let me post a suggestion to its' developer. I am not sure if they will add it.

Posted -

May be you can add your own comment/suggestion there to increase demand ;-)

Update: Nginx-Helper latest release added support for cache-purge using solution mentioned here:

Its not as good as purging by ngx_cache_purge module but can be used till other solution becomes available.