Skip to content

Instantly share code, notes, and snippets.

@luisherranz
Created September 18, 2018 15:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luisherranz/0c2e8882909468b8524da74cdf100b39 to your computer and use it in GitHub Desktop.
Save luisherranz/0c2e8882909468b8524da74cdf100b39 to your computer and use it in GitHub Desktop.
Nginx conf file for WordPress + KeyCDN
server {
listen 80;
listen [::]:80;
root /var/www/html;
index index.php index.html index.htm;
server_name www.domain.io origin.www.domain.io;
set $cache_path $request_uri;
client_max_body_size 100M;
# bypass cache for POST requests
if ( $request_method = POST ) {
set $cache_path 'nocache';
}
# don't cache login/admin URLs
if ($request_uri ~* "/(wp-login.php|wp-admin|login.php|backend|admin)"){
set $cache_path 'nocache';
}
# don't cache if there is a cookie called PHPSESSID
if ($http_cookie ~* "PHPSESSID"){
set $cache_path 'nocache';
}
#Don't cache if there is a cookie called wordpress_logged_in_[hash]
if ($http_cookie ~* "wordpress_logged_in_"){
set $cache_path 'nocache';
}
# 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 $cache_path 'nocache';
}
# bypass cache if query string not empty
if ( $query_string ) {
set $cache_path 'nocache';
}
if (!-e $request_filename) {
rewrite ^.*$ /index.php last;
}
location / {
try_files $uri $uri/ /index.php?q=$uri&$args;
if ($cache_path = 'nocache') {
expires -1;
add_header Cache-Control no-cache;
}
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
if ($cache_path = 'nocache') {
expires -1;
add_header Cache-Control no-cache;
}
if ($cache_path != 'nocache') {
expires +1h;
}
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
log_not_found off;
add_header Cache-Control "max-age=604800, stale-while-revalidate=86400, stale-if-error=604800";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment