502 bad gateway after change nginx config for w3 total cache

You can modify cache key to include user-agent. For example:

fastcgi_cache_key “$scheme$request_method$host$request_uri$rt_session$http_user_agent”;

Above might be very inefficient so its better to do something like below:

  
set $rt_mobile "no";  

if ($http_user_agent ~* '(iPhone|iPod|android|blackberry)') {  
        set $rt_mobile "yes";  
}  

fastcgi_cache_key "$scheme$request_method$host$request_uri$rt_session$rt_mobile";  

Above will create 2 version of every page - one for mobiles and other for non-mobile devices.

You can tweak line if ($http_user_agent ~* ‘(iPhone|iPod|android|blackberry)’) to add/remove desired mobile agents to the list.

But our recommended way of servicing mobile site is using responsive themes - https://rtcamp.com/responsive-web-design/

The theme I use totally support responsive design. I bought it here http://shoppica-wordpress.com/

I don’t know why the issue just happen only with homepage. Other pages work very well.

Could you please tell me where can I put the code?

set $rt_mobile = "no";  

if ($http_user_agent ~* '(iPhone|iPod|android|blackberry)') {  
        set $rt_mobile = "yes";  
}  

fastcgi_cache_key "$scheme$request_method$host$request_uri$rt_session$rt_mobile";

I try to put inside the file /etc/nginx/sites-available/newgirl.vn but it make nginx test failed.

Many thanks and Happy New Year,

Thanks for wishes. Wish you happy new year too. :slight_smile:

Better put above code in php-location block. You can replace line:

fastcgi_cache_key “$scheme$request_method$host$request_uri$rt_session”;

If nginx test fails, please paste output it generates (output of command nginx -t)

My code looks like this now: (from line 41)

...  
set $rt_mobile = "no";  

	if ($http_user_agent ~* '(iPhone|iPod|android|blackberry)') {  
        set $rt_mobile = "yes";  
	}  
	  
	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$rt_mobile";  
...  

And the test failed because:
nginx: [emerg] invalid number of arguments in “set” directive in /etc/nginx/sites-available/newgirl.vn:41

Remove equal to sign from set statements.

Change set $rt_mobile = “no”; to set $rt_mobile “no”; and

Change set $rt_mobile = “yes”; to set $rt_mobile “yes”;

Hurrah! Nginx works correctly now. Sorry I am just a business guy and start to learn IT for a while, so can not figure it out :smiley:

I just reload nginx and the homepage works well now on my fone. The issue may be solved :slight_smile: I will wait for a day and test it again on multiple devices to see.

Such a great start for the new year! I wish you and rtCamp the best luck and success for this 2014

Many thanks,

Thanks for your feedback and wishes. Wish you happy new year too. :slight_smile:

I am marking this issue as resolved for now.

Feel free to open new support request if you need any help.