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.

Hi I installed w3 total cache on my nginx powered site, I'm having errors however. For example when I enable page caching disk:enhanced I get:

It appears Page Cache URL rewriting is not working. If using apache, verify that the server configuration allows .htaccess or if using nginx verify all configuration files are included in the configuration.

When I choose page caching disk:basic no error is displayed.

Here is the config for the server in /etc/nginx/nginx.conf:

http://pastebin.com/SEdnyHbf

If it makes any difference, I have multiple sites installed, all in /srv/www

I can see that W3 Total Cache is creating a separate nginx.conf for each website, e.g. /srv/www/mysite.com/public_html/nginx.conf and within it there are the rules of W3 Total Cache, pasted here: http://pastebin.com/dANPVMJk

I would like to have a setup that works for any number of websites I will add. Clients on my server need to be free to update their W3 Total Cache configuration as they wish without the server needing to be restarted. Also they might opt not to use the plugin at all.

share|improve this question

put on hold as off-topic by birgire, Johannes Pille, Pieter Goosen, ialocin, TheDeadMedic 2 days ago

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

    
My recommendation would be to install APC instead and use that for page caching rather than the disk. It's a bit faster, and less to configure. –  EAMann Jun 5 '12 at 15:20

2 Answers 2

up vote 0 down vote accepted

Your main configuration file at /etc/nginx/nginx.conf has some pitfalls and it shouldn't be that complex. Let me share a simpler version for you to start with...

server {
  server_name www.yourdomain.com;
  # please replace the root path
  # ex: /srv/www/mysite.com/public_html
  root /path/to/wordpress/installation;
  index index.php;

  # please update the path to W3 Total Cache's configuration
  # ex: /srv/www/mysite.com/public_html/nginx.conf
  include /path/to/wordpress/installation/nginx.conf;

  location ~* \.php$ {
     try_files       $uri =404;
     fastcgi_index   index.php;
     fastcgi_pass    127.0.0.1:9000;
     include         fastcgi_params;
     fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
  }

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

Depending on your setup, you may modify the above (or add a few more lines of code). For example, if you access your site with www in front, then you might want to add another server block like this...

server {
  server_name yourdomain.com;
  return 301 $scheme://www.yourdomain.com$request_uri;
}

Whenever you update your W3 Total Cache settings, please always remember to restart or reload your Nginx server. Otherwise, Nginx would still be serving the previous configurations from W3 Total Cache. (I like Apache here that supports on the fly processing of htaccess rules.)

The above configuration has been tested on CentOS, Ubuntu and Debian (latest versions) with the latest Nginx and PHP-FPM. It wouldn't work, if Apache is used to serve PHP.

Hope this helps.

share|improve this answer
    
Since I have more than one site on the server how should I modify the above to cater for all the sites? –  drtanz Jun 7 '12 at 8:06
    
@drtanz If you name this file as yourdomain.com.conf, please copy this file to anotherdomain.com.conf. Then replace yourdomain.com with anotherdomain.com in three places. Then replace the /path/to/wordpress/installation/. If you use W3 Total Cache in anotherdomain.com too, then replace /path/to/wordpress/installation/nginx.conf too. You may automate this (via a bash script or using a sed command) for any number of sites. –  Pothi Jun 8 '12 at 5:37

You need to set the path to your .conf file in the plugin general options then let W3 Total write to the file.

I would create a w3-total.conf file in /etc/nginx then chown it www-data:www-data so w3 can write to it then add it as an include in your server file.

Update:

To answer your additional questions. If all your sites are using the same W3 Total Cache settings give them all the same path to the configuration file and include it inside all your server blocks.

For simplification I like to keep all my server blocks in 1 file then include a global wp.conf and a global w3-total.conf.

server {
   listen 80; 
    server_name domain1.com www.domain1.com;
    root /srv/www/domain1;
    index index.php index.html index.htm;

    include w3-total.conf;
    include wp.conf;    
}
server {
   listen 80; 
    server_name domain2.com www.domain2.com;
    root /srv/www/domain2;
    index index.php index.html index.htm;

    include w3-total.conf;
    include wp.conf;    
}

wp.conf:

error_page 404 = @wordpress;
log_not_found off;

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

rewrite /wp-admin$ $scheme://$host$uri/ permanent;

location @wordpress {
    fastcgi_pass php;
    fastcgi_param SCRIPT_FILENAME $document_root/index.php;
    include fastcgi_params;
    fastcgi_param SCRIPT_NAME /index.php;
}

location ~ \.php$ {
    fastcgi_max_temp_file_size 1M;
    fastcgi_connect_timeout 60;
    fastcgi_send_timeout 180;
    fastcgi_read_timeout 180;
    fastcgi_buffer_size 128k;
    fastcgi_buffers 4 256k;
    fastcgi_busy_buffers_size 256k;
    fastcgi_temp_file_write_size 256k;
    fastcgi_index  index.php;
    fastcgi_pass php;  // php is defined in nginx.conf
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
    try_files $uri @wordpress;
}

Nginx.conf

user www-data;
worker_processes  8;

error_log  /var/log/nginx/error.log;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include mime.types;
    default_type       application/octet-stream;
    access_log         /var/log/nginx/access.log;
    sendfile           on;
    keepalive_timeout  3;
    client_max_body_size 13m;
    # Upstream to abstract backend connection(s) for PHP.
    upstream php {
        server unix:/var/run/php5-fpm.sock;  //Make sure your setup to use sockets in /etc/php5/fpm/pool.d/www.conf
    }

    include sites-enabled/*;
}

The above configuration is for a small Debian Squeeze VPS running php-fpm 5.4 and Nginx 1.2.0

share|improve this answer
    
ok I've created w3-total.conf in /etc/nginx, alongside nginx.conf. I also chowned it, and I can see the plugin is now writing stuff to this file. As I understand the last step would be to include it in nginx.conf, can you tell me how to do that? –  drtanz Jun 4 '12 at 12:30
    
@drtanz You may include it at the top of all other location directives and other rewrite rules. If included in the middle or at the bottom of the rules (or location directives), W3 Total Cache plugin may still complain (aka show the same error). You may include it like include /path/to/w3-total.conf/;. –  Pothi Jun 5 '12 at 11:25
    
I get a location not allowed here in /etc/nginx/w3-total.conf:2 when I add the include under the http seciton in nginx.conf –  drtanz Jun 5 '12 at 13:09
    
I've edited the initial question, maybe I hadn't explained my problem well enough initially. Let me know if you have any further solutions I can try. –  drtanz Jun 5 '12 at 13:20
    
You may include the W3 Total Cache's configuration file only in the server section. Please check my answer below for a simpler version of /etc/nginx/nginx.conf file. Please note you'd still need http section in that file. HTH. –  Pothi Jun 5 '12 at 19:39

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