I'm trying to convert the following Apache rewrites to Nginx :

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} =""
RewriteCond %{REQUEST_URI} \/$
RewriteCond %{HTTP_COOKIE} !(comment_author|wp\-postpass|w3tc_logged_out|wordpress_logged_in|wptouch_switch_toggle) [NC]
RewriteCond "%{DOCUMENT_ROOT}/wp-content/cache/page_enhanced/%{HTTP_HOST}/%{REQUEST_URI}/_index.html" -f
RewriteRule .* "/wp-content/cache/page_enhanced/%{HTTP_HOST}/%{REQUEST_URI}/_index.html" [L]

What I have done is :

# Set a variable to work around the lack of nested conditionals
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 'no cache';
if ($query_string != "") {
    set $cache_uri 'no 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|index\.php|wp\-comments\-popup\.php|wp\-links\-opml\.php|wp\-locations\.php)") {
    set $cache_uri "no cache";

# Don't use the cache for logged in users or recent commenters
if if ($http_cookie ~* "comment_author|wp\-postpass|w3tc_logged_out|wordpress_logged_in|wptouch_switch_toggle") {
    set $cache_uri 'no cache';

 # If the cache file does not exist, pass it of to apache for processing
location / {
    try_files /wp-content/cache/page_enhanced/example.com/$cache_uri/_index.html @backend;

# Pass off php requests to Apache
location ~* \.php$ {           
    include /usr/local/etc/nginx/proxypass.conf;

# Pass off php requests to Apache
location @backend {
    include /usr/local/etc/nginx/proxypass.conf;

What I am wondering is am I missing anything? or done something wrong?

closed as off-topic by mrwweb, birgire, Pieter Goosen, Nicolai, Johannes Pille Aug 29 '14 at 12:22

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Your question should be specific to WordPress. Generic PHP/JS/HTML/CSS questions might be better asked at Stack Overflow or another appropriate site of the Stack Exchange network. Third party plugins and themes are off topic." – mrwweb, birgire, Pieter Goosen, Nicolai, Johannes Pille
If this question can be reworded to fit the rules in the help center, please edit the question.

  • W3 Total Cache integrates well with Nginx and comes with its own Nginx configuration that can be included with a particular site's vhost entry. So, I wonder why convert something that is already done by W3TC. Please look for nginx.conf file in your site, that may reside in the WordPress root directory. – Pothi Kalimuthu Sep 2 '13 at 13:39
  • The nginx.conf file does not exist. W3 Total Cache plugin detects that Apache is running - thus gives me the rewrites for that webserver instead. I am using Nignx in front of Apache - not a Nginx/PHP-FPM solution. – Dave Sep 3 '13 at 0:17

Based on your comment, here's the solution for an Nginx-Apache stack with "disk: enhanced" page cache method in W3 Total Cache plugin...

location / {
    error_page 418 = @cachemiss;
    recursive_error_pages on;

    if ($request_method = POST) { return  418; }

    if ($query_string != "") { return 418; }

    if ($request_uri ~* "(\/wp-admin\/|\/xmlrpc.php|\/wp-(app|cron|login|register|mail)\.php|wp-.*\.php|index\.php|wp\-comments\-popup\.php|wp\-links\-opml\.php|wp\-locations\.php)") { return 418; }

    if ($http_cookie ~* "comment_author|wp\-postpass|w3tc_logged_out|wordpress_logged_in|wptouch_switch_toggle") { return 418; }

    try_files "/wp-content/cache/page_enhanced/$host/$uri/_index.html" =418;

    # optional code
    # expires 30m;
    # add_header "X-W3TC-Cache" "HIT :)";

location @cachemiss {
    # pass the requests to backend (Apache)

    # optional header
    # add_header "X-W3TC-Cache" "Miss :(";

# other directives
# for example
location ~* \.php$ {
    # pass PHP requests to Apache

# another example
location /wp-admin {
    # pass requests to Apache

The above solution follows the best practices of using an if statement in Nginx and works correctly, when modified, for WPSC. I hope that helps.

Not the answer you're looking for? Browse other questions tagged or ask your own question.