Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

nginx 1.2.7 + WP 3.5.1 + W3TC 0.9.2.8.

Rewrite rules generated by the plugin simply don't work, server returns 404 after including them in nginx server config. Or maybe I'm doing something wrong?

Here is my nginx.conf:

user www-data;
worker_processes 2;
pid /var/run/nginx.pid;

events {
    worker_connections 1024;
}

http {
    include /etc/nginx/mime.types;
    index index.html index.htm index.php;
    default_type application/octet-stream;

    sendfile on;
    tcp_nopush on;

    log_format main '$remote_addr - $remote_user [$time_local] '
        '"$request" $status $body_bytes_sent "$http_referer" '
        '"$http_user_agent" "$http_x_forwarded_for"';

    access_log /var/log/nginx/access.log main;
    error_log /var/log/nginx/error.log warn;

    include /etc/nginx/conf.d/*.conf;
}

Here is my virtual server config:

server {
    listen 80;

    server_name example.com www.example.com;
    root /var/www/example.com;

    access_log /var/log/nginx/example.access.log;
    error_log /var/log/nginx/example.error.log;

    location / { try_files $uri $uri/ /index.php?$args; }

    location ~ \.php {
        try_files $uri =404;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    include /var/www/example.com/nginx.conf;

}

And finally config generated by W3TC (included in server's config):

# BEGIN W3TC Minify cache
location ~ /wp-content/cache/minify.*\.js$ {
    types {}
    default_type application/x-javascript;
    expires modified 2628000s;
    add_header X-Powered-By "W3 Total Cache/0.9.2.8";
    add_header Vary "Accept-Encoding";
    add_header Pragma "public";
    add_header Cache-Control "max-age=2628000, public";
}
location ~ /wp-content/cache/minify.*\.css$ {
    types {}
    default_type text/css;
    expires modified 2628000s;
    add_header X-Powered-By "W3 Total Cache/0.9.2.8";
    add_header Vary "Accept-Encoding";
    add_header Pragma "public";
    add_header Cache-Control "max-age=2628000, public";
}
location ~ /wp-content/cache/minify.*js\.gzip$ {
    gzip off;
    types {}
    default_type application/x-javascript;
    expires modified 2628000s;
    add_header X-Powered-By "W3 Total Cache/0.9.2.8";
    add_header Vary "Accept-Encoding";
    add_header Pragma "public";
    add_header Cache-Control "max-age=2628000, public";
    add_header Content-Encoding gzip;
}
location ~ /wp-content/cache/minify.*css\.gzip$ {
    gzip off;
    types {}
    default_type text/css;
    expires modified 2628000s;
    add_header X-Powered-By "W3 Total Cache/0.9.2.8";
    add_header Vary "Accept-Encoding";
    add_header Pragma "public";
    add_header Cache-Control "max-age=2628000, public";
    add_header Content-Encoding gzip;
}
# END W3TC Minify cache
# BEGIN W3TC Page Cache cache
location ~ /wp-content/cache/page_enhanced.*html$ {
    expires modified 3600s;
    add_header X-Powered-By "W3 Total Cache/0.9.2.8";
    add_header Vary "Accept-Encoding, Cookie";
    add_header Pragma "public";
    add_header Cache-Control "max-age=3600, public";
}
location ~ /wp-content/cache/page_enhanced.*gzip$ {
    gzip off;
    types {}
    default_type text/html;
    expires modified 3600s;
    add_header X-Powered-By "W3 Total Cache/0.9.2.8";
    add_header Vary "Accept-Encoding, Cookie";
    add_header Pragma "public";
    add_header Cache-Control "max-age=3600, public";
    add_header Content-Encoding gzip;
}
# END W3TC Page Cache cache
# BEGIN W3TC Browser Cache
gzip on;
gzip_types text/css text/x-component application/x-javascript application/javascript text/javascript text/x-js text/richtext image/svg+xml text/plain text/xsd text/xsl text/xml image/x-icon;
location ~ \.(css|htc|js|js2|js3|js4)$ {
    expires 2628000s;
    add_header Pragma "public";
    add_header Cache-Control "max-age=2628000, public";
    add_header X-Powered-By "W3 Total Cache/0.9.2.8";
}
location ~ \.(html|htm|rtf|rtx|svg|svgz|txt|xsd|xsl|xml)$ {
    expires 3600s;
    add_header Pragma "public";
    add_header Cache-Control "max-age=3600, public";
    add_header X-Powered-By "W3 Total Cache/0.9.2.8";
    try_files $uri $uri/ $uri.html /index.php?$args;
}
location ~ \.(asf|asx|wax|wmv|wmx|avi|bmp|class|divx|doc|docx|eot|exe|gif|gz|gzip|ico|jpg|jpeg|jpe|json|mdb|mid|midi|mov|qt|mp3|m4a|mp4|m4v|mpeg|mpg|mpe|mpp|otf|odb|odc|odf|odg|odp|ods|odt|ogg|pdf|png|pot|pps|ppt|pptx|ra|ram|svg|svgz|swf|tar|tif|tiff|ttf|ttc|wav|wma|wri|xla|xls|xlsx|xlt|xlw|zip)$ {
    expires 31536000s;
    add_header Pragma "public";
    add_header Cache-Control "max-age=31536000, public";
    add_header X-Powered-By "W3 Total Cache/0.9.2.8";
}
# END W3TC Browser Cache
# BEGIN W3TC Minify core
rewrite ^/wp-content/cache/minify.*/w3tc_rewrite_test$ /wp-content/plugins/w3-total-cache/pub/minify.php?w3tc_rewrite_test=1 last;
set $w3tc_enc "";
if ($http_accept_encoding ~ gzip) {
    set $w3tc_enc .gzip;
}
if (-f $request_filename$w3tc_enc) {
    rewrite (.*) $1$w3tc_enc break;
}
rewrite ^/wp-content/cache/minify/(.+\.(css|js))$ /wp-content/plugins/w3-total-cache/pub/minify.php?file=$1 last;
# END W3TC Minify core
# BEGIN W3TC Page Cache core
set $w3tc_rewrite 1;
if ($request_method = POST) {
    set $w3tc_rewrite 0;
}
if ($query_string != "") {
    set $w3tc_rewrite 0;
}
if ($request_uri !~ \/$) {
    set $w3tc_rewrite 0;
}
if ($http_cookie ~* "(comment_author|wp\-postpass|w3tc_logged_out|wordpress_logged_in|wptouch_switch_toggle)") {
    set $w3tc_rewrite 0;
}
set $w3tc_enc "";
if ($http_accept_encoding ~ gzip) {
    set $w3tc_enc _gzip;
}
if (-f "$document_root/wp-content/cache/page_enhanced/$http_host/$request_uri/_index.html$w3tc_enc") {
  set $w3tc_rewrite 0;
}
if ($w3tc_rewrite = 1) {
    rewrite .* "/wp-content/cache/page_enhanced/$http_host/$request_uri/_index.html$w3tc_enc" last;
}
# END W3TC Page Cache core

Server returns 404. What's wrong? Desperate for your help, Wordpress Answers.

share|improve this question

put on hold as off-topic by mrwweb, ialocin, Johannes Pille, birgire, Pieter Goosen yesterday

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, ialocin, Johannes Pille, birgire, Pieter Goosen
If this question can be reworded to fit the rules in the help center, please edit the question.

2 Answers 2

up vote 0 down vote accepted

Solved.

Instead of

if (-f "$document_root/wp-content/cache/page_enhanced/$http_host/$request_uri/_index.html$w3tc_enc") {
  set $w3tc_rewrite 0;
}

should be

if (!-f "$document_root/wp-content/cache/page_enhanced/$http_host/$request_uri/_index.html$w3tc_enc") {
  set $w3tc_rewrite 0;
}

in W3 TC config.

share|improve this answer

W3 Total Cache and Quick Cache and the like work very well with Apache. As Wordpress wasn't made out of the box to be graceful with nginx, you'll run into problems due to the different rewrite rules being involved.

Try this plugin: nginx Compatibility Plugin and that should make your cache work with nginx + Wordpress.

If you find that single pages are still not being cached even when using the plugin, then try DB Cache Reloaded.

Cheers!

share|improve this answer
    
Doesn't work. From the description, nginx Compatibility Plugin is not supposed to solve this kind of problem. –  Michael Mar 7 '13 at 17:12

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