Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WordPress config with WP Super Cache for Nginx
# /etc/nginx/snippets/wordpress/common.conf
index index.php;
# Deny all attempts to access hidden files such as .htaccess, .htpasswd, .DS_Store (Mac).
location ~ /\. {
deny all;
}
# Block PHP files in uploads, content, and includes directory.
location ~* /(?:uploads|files|wp-content|wp-includes)/.*\.php$ {
deny all;
}
# Autoptimize combined/minified files
location /wp-content/cache/autoptimize/ {
expires 1y;
}
# Regular content
location /wp-content/uploads/ {
expires 1w;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass php7;
}
location / {
try_files $cachefile $uri $uri/ /index.php?$args;
}
# /etc/nginx/snippets/wordpress.conf
include /etc/nginx/snippets/wordpress/wp-super-cache.conf;
include /etc/nginx/snippets/wordpress/common.conf;
# /etc/nginx/snippets/wordpress/wp-super-cache.conf
set $cache_uri $request_uri;
# POST requests and urls with a query string should always go to PHP
if ($request_method = POST) {
set $cache_uri 'null cache';
}
if ($query_string != "") {
set $cache_uri 'null cache';
}
# 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|wp-comments-popup.php
|wp-links-opml.php|wp-locations.php|sitemap(_index)?.xml
|[a-z0-9_-]+-sitemap([0-9]+)?.xml)") {
set $cache_uri 'null cache';
}
# Don't use the cache for logged in users or recent commenters
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_logged_in") {
set $cache_uri 'null cache';
}
set $cachefile "/wp-content/cache/supercache/$http_host/$cache_uri/index.html";
if ($https ~* "on") {
set $cachefile "/wp-content/cache/supercache/$http_host/$cache_uri/index-https.html";
}
# /etc/nginx/sites-available/example.com
# Redirect to HTTPS
server {
server_name example.com;
listen 80;
return 301 https://example.com$request_uri;
}
server {
server_name example.com;
listen 443 http2;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
root /var/www/example.com/;
include snippets/wordpress.conf;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment