vrtech77
By:
vrtech77

nginx wordpress w3cache configuration

December 23, 2015 809 views
WordPress LEMP Nginx Caching CentOS

Hi,
I have followed the below links to install LEMP stack on my droplet.

https://www.digitalocean.com/community/tutorials/how-to-install-linux-nginx-mysql-php-lemp-stack-on-centos-7

https://www.digitalocean.com/community/tutorials/how-to-set-up-nginx-server-blocks-on-centos-7

I was able to follow the steps successfully and get my sites up and running.
I also followed a guide to set up a swap memory. Viewing my droplet graphs I can see that a lot of processing is going on. That I believe is due to multiple new WordPress sites hosted on that droplet and crawlers fetching that new data. I was not able to enable w3cache on any of those WordPress sites. It would be helpful if you can guide me to set up w3cache and reduce some load on my droplet.

Here is my current config file for one of my website. All other WordPress configs are almost similar:

server {
listen 80;
server_name example.com www.example.com;

# note that these lines are originally from the "location /" block
root /var/www/example.com/html;
index index.php index.html index.htm;

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

# Add trailing slash to */wp-admin requests.
rewrite /wp-admin$ $scheme://$host$uri/ permanent;

# Directives to send expires headers and turn off 404 error logging.
location ~* ^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
access_log off; log_not_found off; expires max;
}

error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}

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

Thanks.

1 Answer

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. 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.

# 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
Have another answer? Share your knowledge.