502 bad gateway after change nginx config for w3 total cache

Hello support,

Here’s what I did:

  • follow your tutorial http://rtcamp.com/easyengine/ setup fresh wordpress on ec2 amazon
  • Increase php limit http://rtcamp.com/tutorials/php/increase-script-execution-time/
  • Install w3 totalcache and memcache
  • replace the code for /etc/nginx/sites-available/example.com (details code below)
  • restart nginx [nginx -t && service nginx reload] => I got 502 bad gateway; restart nginx or my ec2, but no hope; replace my old code (without w3cache config) it work again.
    What did I do wrong? please help

server {

server_name newgirl.vn http://www.newgirl.vn;

access_log /var/log/nginx/newgirl.vn.access.log rt_cache;
error_log /var/log/nginx/newgirl.vn.error.log;

root /var/www/newgirl.vn/htdocs;
index index.php index.htm index.html;

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’;
}

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 $cache_uri ‘null cache’;
}

Don’t use the cache for logged in users or recent commenters

if ($http_cookie ~* “comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_logged_in”) {
set $cache_uri ‘null cache’;
}

Use cached or actual file if they exists, otherwise pass request to WordPress

location / {
try_files /wp-content/cache/page_enhanced/${host}${cache_uri}_index.html $uri $uri/ /index.php?$args ;
}

location ~ ^/wp-content/cache/minify/[^/]+/(.*)$ {
try_files $uri /wp-content/plugins/w3-total-cache/pub/minify.php?file=$1;
}

location = /favicon.ico { log_not_found off; access_log off; }
location = /robots.txt { log_not_found off; access_log off; }

location ~ .php$ {
try_files $uri /index.php;
include fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_read_timeout 600;
}

Cache static files for as long as possible

location ~* .(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|css|rss|atom|js|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
expires max; log_not_found off; access_log off;
}

}

Many thanks,

As you are using easyengine, why don’t you use easyeninge only to create nginx config with w3 total cache:

ee site create wp w3tc example.com

For more details, please check - http://rtcamp.com/easyengine/docs/site/create/single-site/wordpress-single-site-w3-total-cache/

I did not see that post at the first place. Thanks for your instruction.

I just reinstall and follow your advice. But now I got:

nginx: [alert] mmap (MAP_ANON|MAP_SHARED, 30408704) failed (12: Cannot allocate memory)
nginx: configuration file /etc/nginx/nginx.conf test failed

Many thanks,

How much RAM your server has?

You can try reducing RAM requirement for fastcgi_cache

Open file /etc/nginx/conf.d/fastcgi.conf

Find line:

fastcgi_cache_path /var/run/nginx-cache levels=1:2 keys_zone=WORDPRESS:500m inactive=60m;

Change 500m to 50m

So that line looks like:

fastcgi_cache_path /var/run/nginx-cache levels=1:2 keys_zone=WORDPRESS:50m inactive=60m;

I use Amazon ec2 micro instance ram 613mb

I check file /etc/nginx/conf.d/fastcgi.conf default line is keys_zone=WORDPRESS:29m , not 500m. The weird thing is I just run nginx -t again and the error dissapear.

I have run 2 www.blitz.io test and the result was bad:
test 1: everything the same before i get your instruction
test 2: i change keys_zone=WORDPRESS:50m and memcache -m 200 (instead of -m 1024 like the instruction)
According Blitz: This rush generated 53 successful hits in 57.60 seconds and we transferred 1.51 MB of data in and out of your app. The average hit rate of 0.92/second translates to about 79,506 hits/day.
The average response time was 777 ms.
You’ve got bigger problems, though: 99.74% of the users during this rush experienced timeouts or errors!

Is it weird when I got that result? Many thanks again for your help

Regarding blitz, looks like your cache wasn’t working.

Can you verify if cache is working in the first place?
Check http://rtcamp.com/wordpress-nginx/tutorials/checklist/

I just made the cache test, please take a look at 4 pictures attached.

I can not run content cache cause some error with PHP. I think it might be the problem we’r finding:
stop: unknow instance

Attachment Link(s):

http://rtcamp.com/wp-content/uploads/rtMedia/topics/52023/2013/11/1.jpg

http://rtcamp.com/wp-content/uploads/rtMedia/topics/52023/2013/11/2.jpg

http://rtcamp.com/wp-content/uploads/rtMedia/topics/52023/2013/11/3.jpg

http://rtcamp.com/wp-content/uploads/rtMedia/topics/52023/2013/11/4.jpg

hi Rahul,

I just reinstall again everything. No errors now, but the Blitz result still not good:

This rush generated 1,188 successful hits in 59.95 seconds and we transferred 2.27 MB of data in and out of your app. The average hit rate of 19.82/second translates to about 1,712,027 hits/day. The average response time was 485 ms. You've got bigger problems, though: 94.33% of the users during this rush experienced timeouts or errors!

I run the cache test and you'r right about cache doesn't work: - curl http://example.com/wordpress-post-url/ | tail -n 4 && echo -e =>> (see the pic attached) - service php5-fpm stop ==> I got bad gateway 502 - curl -I http://example.com/wp-content/uploads/2012/09/wow.png ==> worked

Do you have any ideas why my cache not work? since I follow correctly the instruction: install wordpress with w3 total cache; install memcache; config w3 total cache.

Thank you,

Attachment Link(s):

http://rtcamp.com/wp-content/uploads/rtMedia/topics/52097/2013/11/5.jpg

Your pages are not getting cached. Cache should work before you start load-testing.

Can you please send cache config here?

We use fastcgi_cache everywhere so it could be the case that W3TC rules needs update.

Can you try fastcgi-cache as well - http://rtcamp.com/easyengine/docs/site/create/single-site/wordpress-single-site-fastcgi-cache/ ?

Thank you Rahul! You are awesome

I tried fastcgi-cache wordpress single site and the result was amazing. My site can serve 44 million hits/day.

@minhhuy2212

Glad to know that. :slight_smile:

Hi Rahul,

I just came up with another 2 issues ~~ I think they are just small ones and you can easily help me to fix.

First: Can you visit my homepage http://newgirl.vn via Iphone (or another smartfone)? you will see the fone browser try to load the page again and again but it takes forever loading & the result just blank page. But if I go to another sub-page for ex: http://newgirl.vn/shop , it works well.
I believe the reason is cache config since when I purge cachec from Nginx helper, the fone browser works again. but after amount of time (about 3-4 hours), this issue happen again and I don’t want to purge cache again & again for that issue.
The Internet also tells the same problem between responsive website and cache config. I follow them and config W3total cache -> Enable User agent group for my theme. But no hopes.
SO my question is do I need to config anything in Nginx or cache file?

Second, please check the screenshot with memcache issue

Many thanks, Rahul!

Attachment Link(s):

https://rtcamp.com/wp-content/uploads/rtMedia/topics/53232/2013/12/memcache.jpg

First-Issue
Looks like you are using some plugin which detects mobile browsers and load different template.

Either remove those mobile replated plugins. Or change fastcgi-config to hold add user-agent related info to caching key. That way, there will be multiple versions of cached content based on devices.

Name of variable/cookie might be found in that plugins documentation.

Second-Issue
Did you install memcache? Easyengine doesn’t install memcache I guess (as of now)

You can install it by using - http://rtcamp.com/tutorials/php/memcache/

Can you please check - https://github.com/rtCamp/easyengine/issues/91 for second issue?

I assume memcache is already there. So please check only if its running.

Hi Rahul,
Second Issue
I checked. It turned out instead of setting “-m 1024” like your instruction, I only set “-m 256”. I’ve set it again and restart memcached. Everything works great now. Thank you!

First Issue
I tried and disable one by one of installed plugins. My site only has Woocommerce, W3 total cache and Nginx Helper now, but no hopes to fix that issue.
Can you please give me instruction to config fastcgi cache?

Many thanks,

Hi Rahul,

Best wishes for Christmas and the new year. You may enjoy the holiday now.

After holiday, if you have time please take a look for my first issue mentioned above.

Many thanks,

Hey thanks for your wishes. Merry Christmas and Happy New Year to you and your family. :slight_smile:

Sorry for delay in reply.

For woocommerce and fastcgi-caching issue - try https://gist.github.com/rahul286/dc64ae84c97868b862c4#file-woo-nginx-conf-L44-L55

WooCommerce sends a session cookie on every page. Above config is experimental so if you face any issue, just let us know.

Thank you for your wishes :slight_smile:

I just made the change like your githup in the file /etc/nginx/sites-available/newgirl.vn 2 days ago. But unfortunately, this still not works. If I go to the homepage via my Iphone, the blank page still load and load over again until I Purge the cache.

FYI, I met the same issue before when I install simple nginx & W3 total cache. I must empty the cache via W3 total to solve the issue, but it will happen again after few hours. Through google, I found solution for that by enabling User Agent group in W3 Total cache config. So the solution for Fastcgi cache may be the same?

My file /etc/nginx/sites-available/newgirl.vn look like this now:

server {  

	server_name newgirl.vn www.newgirl.vn;  

	access_log   /var/log/nginx/newgirl.vn.access.log rt_cache;  
	error_log    /var/log/nginx/newgirl.vn.error.log;  

	root /var/www/newgirl.vn/htdocs;  
	index index.php index.htm index.html;  

	fastcgi_cache_use_stale error timeout invalid_header http_500;  
   
	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;  
	}     
   
	if ( $cookie_woocommerce_items_in_cart = "1" ){  
		 set $skip_cache 1;  
	}  
   
	# Don't cache uris containing the following segments  
	if ($request_uri ~* "(/shop.*|/cart.*|/my-account.*|/checkout.*|/addons.*|/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.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 / {  
		try_files $uri $uri/ /index.php?$args;  
	}     
   
	  
   
	location ~ \.php$ {  
	 	set $rt_session "";  
	 	  
		if ($http_cookie ~* "wc_session_cookie_[^=]*=([^%]+)%7C") {  
               		set $rt_session wc_session_cookie_$1;  
       		}	  
	  
		if ($skip_cache = 0 ) {  
			more_clear_headers "Set-Cookie*";  
			set $rt_session "";  
		}  
		  
	        fastcgi_cache_key "$scheme$request_method$host$request_uri$rt_session";  
   
		try_files $uri =404;  
		include fastcgi_params;  
		fastcgi_pass php;  
   
		fastcgi_cache_bypass $skip_cache;  
	        fastcgi_no_cache $skip_cache;  
   
		fastcgi_cache WORDPRESS;  
		fastcgi_cache_valid  60m;  
	}  
   
	include /etc/nginx/common/wpcommon.conf;  
	include /etc/nginx/common/locations.conf;  
   
}