W3 Total Cache configuration for Nginx-Apache server stack

During the past 24 hours, there were two people had the following situation and were looking for a solution. Their use-case is…

I’m currently using a Nginx frontend / Apache backend setup. The W3 Total Cache plugin detects Apache and will only show me the Apache rewrite rules.

Here’s another quote from the other person who was looking for a similar solution…

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

Interesting, but not uncommon. So, I dived in and modified my existing Nginx rules for WP Super Cache plugin and provided a unique solution.

Here’s the full working solution that applies to any Nginx-Apache server stack with W3 Total Cache plugin…


server {
  server_name domainname.com;
  listen 80;

  root "/path/to/wordpress/core";
  index index.php;

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

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

    if ($query_string != "") { 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 all PHP requests to Apache
  }

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

Since, it follows the best practices of using an if statement in Nginx, this code should work flawlessly. You may still need to fill certain blocks in it to make it work for your specific situation.

8 Replies to “W3 Total Cache configuration for Nginx-Apache server stack”

  1. Hi Pothi

    The above solution is awesome, its a fantastic job. However, as you mentioned in the post that you have an existing solution for Wp supercache, would u mind sharing it with us? I will run a mixed SSL site with nginx in the frontend and apache at the back, I have already set it up and working, but the issue is with the supercache+nginx combination.

    I tried W3 Total Cache, but it breaks my site.
    I have checked few solutions, but they are very old, so I am bit wary to use them e.g

    http://stackoverflow.com/questions/884845/nginx-as-front-end-to-apache-to-serve-wp-super-cache-static-files

    https://kura.io/2011/09/30/nginx-config-for-reverse-proxying-wordpress-wp-super-cache-and-keeping-the-load-off-apache2/

    http://tech.nocr.at/tech/how-to-speed-up-wordpress-with-nginx-and-wp-super-cache/

    http://myatus.com/p/faster-wp-super-cache-with-nginx/

    i am really searching for the right answer.

    1. Hi Stephen,

      Glad to know that you find my solution useful.

      I don’t have an existing solution for WP Super Cache plugin with Nginx and Apache. However, you may modify this W3TC configuration to work with WP Super Cache. You may also get some inspiration on how WP Super Cache can be configured with Nginx at https://github.com/pothi/WordPress-Nginx/blob/master/sites-available/wpsc.domainname.tld.conf

      If you need it working with SSL or if you need any other help related to Nginx, please feel free to contact me.

  2. I’m not 100% familiar with the Nginx. Right now my VPS front end Nginx and backed Apache. All the configurations done by another developer. I face the same issue. I have a standard WordPress installation. Can I use this nginx configuration file without modifying or I have to modify it?

    In its,

    server {
    server_name domainname.com;

    I have to replace my domain name? If so how do I find server_name.

    Could you please help me.

    1. Can I use this nginx configuration file without modifying or I have to modify it?

      No, no one can use this without modifying it.

      I have to replace my domain name?

      Actually, lot more changes to be done, rather than just domain name, in order to make this config usable. Without a certain degree of knowledge of Nginx and how proxy works in Nginx, it wouldn’t be possible. You may start knowing more if you test drive various configurations mentioned at my Github repo for Nginx.

      Could you please help me.

      Of course, I could. You may always get my (paid) services by contacting me at https://www.tinywp.in/contact/ .

      1. Thank you pothi your reply. Next month I’m going to move this VPS provider. Therefore, I rather like to get your service after the migration. So if you don’t mind, is it OK if I contact you next month?

Leave a Reply

Your email address will not be published. We use cookies to prevent spam comments. Required fields are marked *

css.php