W3 Total Cache With Nginx - Config

Posted September 19, 2014 48.8k views

I’ve seen a number of questions asked on this subject already, but I’ve yet to find a single quality tutorial, or proper set of answers.

How exactly are we supposed to configure W3 Total Cache with Nginx? I connect via SSH, and have a specific user who has complete access to my wordpress installation. This allowed W3 Total Cache to install, and create an nginx.config file on the root of my domain (/var/www/ I’ve read of a number of things you should do from here, with no definitively clear answer on which is best, if any:

-It seems you can add an include, so that you have both your server block config, and your root config. Does the include go in the root to include the server block, or the other way around?

-Another thing I recently discovered, is that there is a path to config option at the bottom of the general tab for W3 Total Cache. It says if you leave it blank, the default will be used, which appears to be the root of your website. Could I add “/etc/nginx/sites-available/” there and simply have W3 write to my server block config? Is this a better route to take?

-I also see a ton of nginx config files that already include various W3 settings, but they’re all extremely complex looking to me, and I’m afraid of using them as I have a unique setup (Wordpress in a directory of its own, but served on the root), which has already required some odd changes to my try files in the server block config.

Can someone please offer some guidance on how I should set this up? DigitalOcean has amazing community written tutorials, but this specific subject is greatly lacking. Thanks for any advice, it’s much appreciated.

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Submit an Answer
3 answers

If you visit W3 Total Cache will show you all the additional configuration information that you need. Included in that information will be some Nginx configurations which I’ve also listed below. Copy and paste it into your WordPress installation’s Nginx server block (a.k.a VirtualHosts).

# BEGIN W3TC Minify cache
location ~ /wp-content/cache/minify.*\.js$ {
    types {}
    default_type application/x-javascript;
    add_header Vary "Accept-Encoding";
location ~ /wp-content/cache/minify.*\.css$ {
    types {}
    default_type text/css;
    add_header Vary "Accept-Encoding";
location ~ /wp-content/cache/minify.*js\.gzip$ {
    gzip off;
    types {}
    default_type application/x-javascript;
    add_header Vary "Accept-Encoding";
    add_header Content-Encoding gzip;
location ~ /wp-content/cache/minify.*css\.gzip$ {
    gzip off;
    types {}
    default_type text/css;
    add_header Vary "Accept-Encoding";
    add_header Content-Encoding gzip;
# END W3TC Minify 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;
# END W3TC Browser Cache
location ~ \.(ttf|ttc|otf|eot|woff|font.css)$ {
   add_header Access-Control-Allow-Origin "*";
# 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/(.+/[X]+\.css)$ /wp-content/plugins/w3-total-cache/pub/minify.php?test_file=$1 last;
rewrite ^/wp-content/cache/minify/(.+\.(css|js))$ /wp-content/plugins/w3-total-cache/pub/minify.php?file=$1 last;
# END W3TC Minify core
  • Thanks for the info. A few questions, just so I understand what exactly is going on here.

    Are we manually writing to the nginx server block config because W3 doesn’t support doing so by default? I see it could if the directory had permissions, but I don’t think it’s good practice to allow Wordpress access to that directory, which is why I assume we manually add the code.

    I’m about confused as to how the rewrite rules are working with W3. If we change settings in W3, they’re not actually saving, correct? Do we have to manually change the server block file every time we change any setting? Or do the rewrite rules simply allow for the settings to work IF we turn them on? I could really use some help understanding this bit.

    I’ve read that nginx gzip is much more optimized than the W3 gzip solution, do I need to disable the gzip for W3 in my server block if I plan on using the config that I’ve already set up in etc/nginx/nginx.conf?

    Again, thanks for the information, you’ve been quite helpful over the last couple of weeks with my questions.

  • With Apache, W3 just drops an .htaccess file into the directory. Since Nginx doesn’t support that you need to manually add it to the configuration file. I believe that you’re correct that the rewrite rules just allow for the settings to work if you turn them on.

  • I assumed that nginx doesn’t support multiple configs, as you can with Apache and .htaccess (which I don’t miss, as that can get quite confusing). The only reason I was confused, was because I saw an article somewhere that mentioned allowing W3 to create the file locally on the root (which does happen), and to include the other file with an “include /path/to/original/config” - no idea if that actually works or not, and I’ve only seen it suggested once, but the manual insertion many many times.

    As for the rewrite rules taking effect only if the settings are on, I’d love to confirm this, so I’ll play around with it and see if I can get a confirmation. I’m not exactly a pro at this, but it shouldn’t be too hard to change some settings, check the file (which it can’t write to, so it shouldn’t matter), and do some tests with Pingdom and Google Page Speed Insights - should let me know how it works.

  • I got it working. Just as anticipated in my original post, my rewrite rules were a bit different do to Wordpress being contained within its own wordpress folder. I now get that the “” url spits out the exact needs for your own setup, which makes things really easy. Thanks again for guiding me along, it makes learning all of this much easier.

  • @asb
    brother, i have installed my server following this tutorial

    and from install tab in w3tc says to write rewrite rules in /var/www/html/nginx.conf

    unfortunately i still cant use disk:enhanced mode

    from server error log i get
    09:29:11 [emerg] 3509#0: “rewrite” directive is not allowed here in /var/www/html/nginx.conf:2

    and if i enable debug mode it shows me page cache error. will not work without rewrite.

    what to do?

    by Justin Ellingwood
    WordPress is the most popular CMS (content management system) in the world. It can be used to get your site or blog off the ground quickly and it provides a nice interface for adding content and modifying the site's design. In this guide, we'll walk through how to install WordPress on Ubuntu 14.04 with an Nginx web server.

I got it working, mostly, but now my css isn’t displaying. I chose manual (auto said it didn’t work), and now my css claims to be minified, but isn’t showing on my site. There are no errors, it’s just behaving as mentioned. I’ve checked online a bit, but I haven’t found anything too useful yet. I’ll keep searching, but if anyone knows the cause, I’d love to hear the fix.

  • Have you tried using a different browser or clearing your browser’s cache?

  • I got it working, and posted, so, I just did in response to one of my earlier responses, and not this, as I’m not used to the community setup yet haha. I had to add /wordpreess to my rewrite rules, as my installation is contained within a Wordpress folder.

I Have WordPress + Wocommerce site on VPS with 12GB RAM & Nginx. When i test my website using it shows 15 seconds delay in my TTFB (Time to First Byte) this was too long.. i am not getting how to reduce this..

I Have Used w3 total cache + MaxCDN also but still this remains same 15 seconds of TTFB.. please suggest me to improve my Spiritual Item online Site Performance

  • I am having this exact issue, did you ever manage to resolve it?

    For me disabling W3 total cache, uninstalling and reinstalling, then enabling features 1 by 1 and then speed testing helped me narrow it down. I had migrated from apache2 and with its Updraft backup so it could have broken something that way around.

    I also had to add the w3 total cache nginx.conf to the wordpress vhost