Created
November 3, 2021 15:38
-
-
Save manzilku/352b2006f94490fcbb1b268f1ce304e9 to your computer and use it in GitHub Desktop.
Nginx Configuration for WP Super Cache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
server { | |
root /var/www/domain.com/html; | |
index index.php index.html index.htm; | |
server_name domain.com www.domain.com; | |
access_log /var/log/nginx/domain.com.access.log; | |
error_log /var/log/nginx/domain.com.error.log; | |
# WP Super Cache START | |
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-location.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'; | |
# Use cached or actual file if it exists, otherwise pass request to WordPress | |
location / { | |
try_files /wp-content/cache/supercache/$http_host/$cache_uri/index-https.html | |
$uri $uri/ /index.php$is_args$args; | |
} | |
location = /favicon.ico { | |
log_not_found off; | |
access_log off; | |
} | |
location = /robots.txt { | |
try_files $uri $uri/ /index.php?$args; | |
log_not_found off; | |
access_log off; | |
} | |
# Cache static files for as long as possible | |
location ~*.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|css|rss|atom|js|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ { | |
expires max; | |
log_not_found off; | |
access_log off; | |
} | |
# WP Super Cache END | |
# WP Security START | |
location ~ ^/(wp-admin|wp-config|wp-login\.php) { | |
allow 192.168.0.1; | |
deny all; | |
include snippets/fastcgi-php.conf; | |
fastcgi_pass unix:/run/php/php7.4-fpm.sock; | |
} | |
location /wp-content/uploads/dlm_uploads { | |
deny all; | |
return 403; | |
} | |
# WP Security END | |
gzip on; | |
gzip_disable "MSIE [1-6]\.(?!.*SV1)"; | |
gzip_vary on; | |
gzip_types text/plain text/css text/javascript image/svg+xml image/x-ic>; | |
location ~ .php$ { | |
include snippets/fastcgi-php.conf; | |
fastcgi_split_path_info ^(.+\.php)(/.+)$; | |
include fastcgi_params; | |
fastcgi_pass unix:/var/run/php/php7.4-fpm.sock; | |
} | |
listen 443 ssl; # managed by Certbot | |
ssl_certificate /etc/letsencrypt/live/domain.com/fullchain.pem; # managed by Certbot | |
ssl_certificate_key /etc/letsencrypt/live/domain.com/privkey.pem; # managed by Certbot | |
include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot | |
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot | |
} | |
server { | |
if ($host = www.domain.com) { | |
return 301 https://$host$request_uri; | |
} # managed by Certbot | |
if ($host = domain.com) { | |
return 301 https://$host$request_uri; | |
} # managed by Certbot | |
listen 80; | |
listen [::]:80; | |
server_name domain.com www.domain.com; | |
return 404; # managed by Certbot | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment