PHP block in nginx with fastcgi_cache

In the fastcgi + nginx tutorial, the block for PHP is:

location ~ .php$ {  
		try_files $uri /index.php;   
		include fastcgi_params;  
		fastcgi_pass unix:/tmp/php-fpm.sock;  

		fastcgi_cache_bypass $skip_cache;  
	        fastcgi_no_cache $skip_cache;  

		fastcgi_cache WORDPRESS;  
		fastcgi_cache_valid  60m;  

However, I get blank pages when I do that. In instead of this code, I’m then using something similar to the one in Wordpress Codex Nginx:

location ~ \.php$ {  
	# Zero-day exploit defense.  
	# Won't work properly (404 error) if the file is not stored on this server, which is entirely possible with php-fpm/php-fcgi.  
	# Comment the 'try_files' line out if you set up php-fpm/php-fcgi on another machine.  And then cross your fingers that you won't get hacked.  
	try_files $uri =404;  

	fastcgi_split_path_info ^(.+\.php)(/.+)$;  
	#NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini  

	include fastcgi_params;  
	fastcgi_index index.php;  
	fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;  
	fastcgi_pass unix:/tmp/php-fpm.sock;  

	fastcgi_cache WORDPRESS;  
	fastcgi_cache_valid  30d;  

This is the only way I found out to avoid blank pages and fastcgi_cache seems to be working. The question is: Am I doing a good thing or the change I was forced to do in the code has any negative point?


You just need to update:

try_files $uri /index.php;


try_files $uri =404;.

Both of above prevents that so-called “zero-day” exploit!

Major part of Nginx page on WordPress Codex is written by me only. I wanted to replace /index.php with =404 for performance reason in each of our tutorials as well.

Finally, there is nothing wrong if you keep either block of codes. Both are safe.

Hello Rahul,

The code I’m already using is:

try_files $uri =404;

But your tutorial suggests:

try_files $uri /index.php;

I couldn’t understand your last message. Do I have to keep “404” or may I change it to index.php, according to your tutorial?

P.S: I did some changes into your code to make it work with my mobile theme. I did it successfully. Thing is, though, the cache is only cleaned automatically for the desktop theme. In order to also clean it for the mobile version, I have to add a “1” at the end of the URL. I need to clean both and

I tried changing this (in purge.php file):

$this->purgeUrl( $homepage_url ); // Clears the homepage.

To this:

$this->purgeUrl( $homepage_url ); // Clears the homepage (for desktop).  
$this->purgeUrl( $homepage_url ) . "1"; //Clears the homepage (for mobile).  

I repeated it similarly for taxonomies, single post, etc, but had no success in the automatically purge (but the manual purge works as expected, which proves that the nginx configuration is okay). As the plugin is written by you, do you have any idea about what can be wrong? Or have any suggestion for me?


I couldn’t understand your last message. Do I have to keep “404″ or may I change it to index.php, according to your tutorial?

Both will work. Use 404. I did not get time to update tutorials but we use “404” everywhere.

Regarding purging different versions, it is going to be tricky. As of now we simply purge entire cache if there is some critical change.

You can modify nginx-helper plugin to purge multiple version by setting up different user-agent string.

What is value of fastcgi_cache_key that you are using?


If a mobile device is detected, $mobile is set to 1. If it is a desktop, nothing is done with $mobile, than it keeps a null value.

Looks nice. :slight_smile:

Can you check if manual purge is working by: and

Before purging make sure you have cached both versions.

Hello Rahul!

I checked it. Yes, manual purge is working for both pages and (there’s no “/” after 1).

I think when you are trying to purge , there might be internal rediect to

Can you try debugging on this line?

Hello Rahul,

I don’t think it is a redirect as if I purge manually with the last “/” I get 404 Not Found. The variable $mobile is just being used to add a a “1” at the end of the URL, not a “/”. Also, when I try opening (without “purge” in the middle and withour the “/” at the end), I can view the mobile content.

Is the line $this->purgePost( $_post_id ) . “1”; correct? I don’t have experience with PHP. I tried to modify the plugin adding the code as I mentioned before, but I don’t know if I did it in the right way.

UPDATE: I’ve edited the plugin code, duplicating the necessary lines according to what I suggested in the other posts. Worked. :slight_smile:


Sorry for delayed reply. Please make sure you maintain copy of your changes locally.

There is a pull-request for nginx-helper there -

This means on next update your changes might get lost. :expressionless:

We will add option to purge multiple variations of page someday in nginx-helper.

Idea is to provide a textbox where people will add suffixes and then nginx-helper with just append url + suffix and create a purge request for each suffix.