Question

W3 Total Cache With Nginx - Config

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/anightinburton.com/public_html). 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/anightinburlington.com” 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.


Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

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.

If you visit your-domain.com/wp-admin/admin.php?page=w3tc_install 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
# BEGIN W3TC CDN
location ~ \.(ttf|ttc|otf|eot|woff|font.css)$ {
   add_header Access-Control-Allow-Origin "*";
}
# END W3TC CDN
# 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

To whom it may concern - you mentioned

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?

The nginx server block config needs to be updated so that it also includes W3TC’s nginx.conf, which is normally located in the site root.

So you would need to update your corresponding server block config, located in /etc/nginx/conf.d, and add

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;

This is as per [https://wordpress.stackexchange.com/questions/54139/w3total-cache-problem-with-nginx](this instructions).

So just to be clear, nginx is not loading nginx.conf in the site root (or any other folder) by default, like apache would load .htaccess. You need to define the include in the server block config. Otherwise W3TC is writing that nginx.conf file for nothing (referring to i.e. the /var/www/html/nginx.conf file)

I Have WordPress + Wocommerce site on VPS with 12GB RAM & Nginx. When i test my website using WebPageTest.org 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