Woocommerce on nginx with fastcgi cache


#1

Hello,
Thank you before for great tutorial.

I have running nginx with wordpress base on your tutorial at https://rtcamp.com/wordpress-nginx/tutorials/single-site/fastcgi-cache-with-purging/. It works well.

When I try to install woocommerce and follow the tutorial at https://rtcamp.com/wordpress-nginx/tutorials/plugins/woocommerce/, I have trouble to make nginx running when adding:

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;  

My fastcgi cache like this:
location ~ .php$ {
try_files $uri /index.php;
include fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;

	fastcgi_cache_bypass $skip_cache;  
        fastcgi_no_cache $skip_cache;  

	fastcgi_cache WORDPRESS;  
	fastcgi_cache_valid  60m;  
}  

Can you help me?

Regards,
Dave


#2

Change your PHP location block with following code:

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 unix:/var/run/php5-fpm.sock;  

fastcgi_cache_bypass $skip_cache;  
fastcgi_no_cache $skip_cache;  

fastcgi_cache WORDPRESS;  
fastcgi_cache_valid 60m;  
}

#3

hello,
Thank you for reply.
But if I just put :

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";</blockquote>  

nginx testconfig failed.
Maybe conflict with: fastcgi_cache_key “$scheme$request_method$host$request_uri”; at the top

Thank you,
dave


#4

@ratmz, can you please post output of following command:
nginx -t


#5

Hello,
This is nginx -t output:

nginx: [emerg] unknown directive "more_clear_headers" in /etc/nginx/sites-enabled/mydomain.com:56 nginx: configuration file /etc/nginx/nginx.conf test failed

My nginx configuration:

# nginx -V nginx version: nginx/1.6.0 built by gcc 4.7.2 (Debian 4.7.2-5) TLS SNI support enabled configure arguments: --with-cc-opt='-g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2' --with-ld-opt=-Wl,-z,relro --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-ipv6 --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-file-aio --with-http_spdy_module --with-http_addition_module --with-http_dav_module --with-http_geoip_module --with-http_gzip_static_module --with-http_image_filter_module --with-http_secure_link_module --with-http_sub_module --with-http_xslt_module --with-mail --with-mail_ssl_module --add-module=/usr/src/nginx/source/nginx-1.6.0/debian/modules/nginx-auth-pam --add-module=/usr/src/nginx/source/nginx-1.6.0/debian/modules/nginx-dav-ext-module --add-module=/usr/src/nginx/source/nginx-1.6.0/debian/modules/nginx-echo --add-module=/usr/src/nginx/source/nginx-1.6.0/debian/modules/nginx-upstream-fair --add-module=/usr/src/nginx/source/nginx-1.6.0/debian/modules/nginx-syslog --add-module=/usr/src/nginx/source/nginx-1.6.0/debian/modules/nginx-cache-purge --add-module=/usr/src/nginx/source/nginx-1.6.0/debian/modules/ngx_http_pinba_module --add-module=/usr/src/nginx/source/nginx-1.6.0/debian/modules/ngx_http_substitutions_filter_module --add-module=/usr/src/nginx/source/nginx-1.6.0/debian/modules/nginx-x-rid-header --with-ld-opt=-lossp-uuid

Thank you,
Dave


#6

It looks like there is error at line 56 of /etc/nginx/sites-enabled/mydomain.com, Can past that file ?
cat /etc/nginx/sites-enabled/mydomain.com


#7

hello,
this is mydomain.com config:

fastcgi_cache_path /var/run/nginx-cache levels=1:2 keys_zone=WORDPRESS:100m inactive=60m; fastcgi_cache_key "$scheme$request_method$host$request_uri"; fastcgi_cache_use_stale error timeout invalid_header http_500; fastcgi_ignore_headers Cache-Control Expires Set-Cookie; server { server_name _; rewrite ^ $scheme://mydomain.com$request_uri redirect; } server { server_name mydomain.com; root /var/www/mydomain.com/htdocs; index index.html index.php;
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;  
}     

# 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|wp-comments-popup.php|wp-links-opml.php|wp-locations.php|sitemap(_index)?.xml|[a-z0-9_-]+-sitemap([0-9]+)?.xml)") {  
	set $skip_cache 1;  
}     

    if ( $arg_add-to-cart != "" ) {   
           set $skip_cache 1;  
    }  

    if ( $cookie_woocommerce_items_in_cart != "0" ) {	  
       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;  
}      
             
    rewrite /wp-admin$ $scheme://$host$uri/ permanent;  

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 unix:/var/run/mydomain.com.sock;  

	fastcgi_cache_bypass $skip_cache;  
        fastcgi_no_cache $skip_cache;  

	fastcgi_cache WORDPRESS;  
	fastcgi_cache_valid  60m;  
}  

location ~ /purge(/.*) {  
    fastcgi_cache_purge WORDPRESS "$scheme$request_method$host$1";  
}	  

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

location = /robots.txt { access_log off; log_not_found off; }  
location ~ /\. { deny  all; access_log off; log_not_found off; }  
    location = /favicon.ico { access_log off; log_not_found off; }  
    location ~* /(?:uploads|files)/.*\.php$ { deny all; }  

}

line 56 error is = more_clear_headers “Set-Cookie*”;
Is there related with nginx ngx_headers_more module?

thanks,
Dave


#8

Hello,
I have fix the problem.

On debian, the different nginx packages with additional module are describe here:

So the fix is easy simple, switch from nginx-full to nginx-extras or compile nginx with header-more-nginx module.

Thanks,
Dave


#9

@ratmz
Glad to know you have solved the problem.


#10