Using caching with Nginx could be a handfull but make a huge difference when it comes to performance on a webpage. This one included.
The method I use is fastcgi. If you only have one site to cache on your server, you put the following four lines in the very top of the vhost config-file. If you have multiple sites to cache, only the top line should go in the vhost config-file, the rest should be added to the “http”-block in the nginx.conf.
fastcgi_cache_path /var/run/nginx-cache levels=1:2 keys_zone=WORDPRESS:200m inactive=360m;
ie;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
fastcgi_cache_use_stale error timeout invalid_header http_500;
fastcgi_ignore_headers Cache-Control Expires Set-Cook
My vhost config-file usually looks like this, with a few modifications here and there.
fastcgi_cache_path /var/run/nginx-cache levels=1:2 keys_zone=WORDPRESS:200m inactive=360m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
fastcgi_cache_use_stale error timeout invalid_header http_500;
fastcgi_ignore_headers Cache-Control Expires Set-Cookie;
server {
listen 80 default_server;
server_name example.com www.example.com;
root /data/www/wordpress;
error_log /var/log/nginx/wordpress
-error.log;
access_log /var/log/nginx/wordpress
-access.log combined;
index index.php;
gzip on;
gzip_http_version 1.1;
gzip_vary on;
gzip_comp_level 6;
gzip_proxied any;
gzip_types text/plain application/javascript application/x-javascript text/javascript text/xml text/css image
/svg+xml;
#fastcgi_cache start
set $no_cache 0;
# POST requests and urls with a query string should always go to PHP
if ($request_method = POST) {
set $no_cache 1;
}
if ($query_string != "") {
set $no_cache 1;
}
# Don't cache uris containing the following segments
if ($request_uri ~* "(/wp-admin/|/xmlrpc.php|/wp-(app|cron|login|register|mail).php|wp-.*.php|/feed/|index.php|w
p-comments-popup.php|wp-links-opml.php|wp-locations.php|sitemap(_index)?.xml|[a-z0-9_-]+-sitemap([0-9]+)?.xml)") {
set $no_cache 1;
}
# Don't use the cache for logged in users or recent commenters
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") {
set $no_cache 1;
}
location / {
# This is cool because no php is touched for static content.
# include the "?$args" part so non-default permalinks doesn't break when using query string
try_files $uri $uri/ /index.php?$args;
}
# location ~ [^/]\.php(/|$) {
location ~ \.php$ {
fastcgi_split_path_info ^(.+?\.php)(/.*)$;
if (!-f $document_root$fastcgi_script_name) {
return 404;
}
include fastcgi.conf;
fastcgi_keep_conn on;
fastcgi_intercept_errors on;
#fastcgi_pass php;
fastcgi_pass unix:/run/php/wordpress
.sock;
fastcgi_buffers 4 256k;
fastcgi_buffer_size 128k;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_index index.php;
fastcgi_cache_bypass $no_cache;
fastcgi_no_cache $no_cache;
if ($no_cache != 1) {
add_header X-CACHE "true";
}
fastcgi_cache WORDPRESS;
fastcgi_cache_valid 200 360m;
}
location ~* \.(png|jpg|jpeg|gif|ico|svg|woff2)$ {
expires 365d;
add_header Cache-Control "public, no-transform";
log_not_found off;
}
location ~* \.(js|css|pdf)$ {
expires 30d;
add_header Cache-Control "public, no-transform";
log_not_found off;
}
location ~ ^/(status|ping)$ {
allow 192.168.10.0/24;
deny all;
access_log off;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/run/php-fpm/wordpress
.sock;
}
include /etc/nginx/wp.d/*conf;
# Load configuration files for the default server block.
include /etc/nginx/default.d/*.conf;
}