Last active
April 16, 2023 22:02
-
-
Save Friz-zy/3dd2a670d8f14eac717e100d0dc7696f to your computer and use it in GitHub Desktop.
Nginx cache example config
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
# Based on articles: | |
# https://ruhighload.com/%D0%9A%D1%8D%D1%88%D0%B8%D1%80%D0%BE%D0%B2%D0%B0%D0%BD%D0%B8%D0%B5+%D1%81+nginx | |
# https://wiki.enchtex.info/practice/nginx/cache | |
# https://gist.github.com/yanmhlv/5612256 | |
# https://nginx.org/en/docs/http/ngx_http_fastcgi_module.html | |
# https://blog.runcloud.io/2017/10/28/nginx-caching-tutorial-wordpress.html | |
# https://www.digitalocean.com/community/tutorials/how-to-setup-fastcgi-caching-with-nginx-on-your-vps | |
# https://easyengine.io/wordpress-nginx/tutorials/single-site/fastcgi-cache-with-purging/ | |
# move cache settings into /etc/nginx/nginx.conf if you want to use cache across many sites | |
# or set unique keys_zone name for each site | |
# /var/run/nginx-cache - path to cache on disk | |
# keys_zone=FASTCGICACHE:100m - unique name and memory pool size for keys dictionary | |
# inactive=6h delete page from cache if there was no requests for it in this period | |
# max_size=1g - limit of cache size on disk | |
fastcgi_cache_path /var/run/nginx-fastcgi-cache levels=1:2 keys_zone=FASTCGICACHE:100m inactive=6h max_size=1g; | |
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; | |
proxy_cache_path /var/run/nginx-proxy-cache levels=1:2 keys_zone=PROXYCACHE:100m inactive=6h max_size=1g; | |
proxy_cache_key "$scheme$request_method$host$request_uri"; | |
proxy_cache_use_stale error timeout invalid_header http_500; | |
proxy_ignore_headers Cache-Control Expires Set-Cookie; | |
# Unfortunately you can't set *cache_valid time as a variable | |
# So you should change time values across the config into necessary | |
# Note: for forums use small cache time like 1-10 seconds | |
# https://deliciousbrains.com/microcaching-wordpress-nginx-improve-server-requests-2400/ | |
server { | |
listen 80; | |
server_name example.com; | |
access_log /var/log/nginx/example.com.access.log; | |
error_log /var/log/nginx/example.com.error.log; | |
root /usr/share/nginx/html; | |
index index.php index.html index.htm; | |
# Protect .git and other hidden files | |
# Left logging for fail2ban | |
location ~ /\. { deny all; } | |
# Static content | |
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)$ { | |
gzip on; | |
gzip_static on; | |
gzip_disable "MSIE [1-6]\.(?!.*SV1)"; | |
gzip_proxied any; | |
gzip_comp_level 6; | |
gzip_buffers 16 8k; | |
gzip_http_version 1.0; | |
gzip_types | |
text/plain | |
text/css | |
text/js | |
text/xml | |
text/x-component | |
text/javascript | |
application/javascript | |
application/x-javascript | |
application/json | |
application/xml | |
application/rss+xml | |
image/svg+xml; | |
expires max; | |
try_files $uri =404; | |
} | |
# | |
# Magic start here | |
# | |
# Enable SSI | |
# https://en.wikipedia.org/wiki/Server_Side_Includes | |
ssi on; | |
# Enable caching for all by default | |
set $skip_cache 0; | |
# Disable cache for all requests except GET and HEAD | |
if ($request_method !~ ^(GET|HEAD)$) { | |
set $no_cache "1"; | |
} | |
# Disable cache for all requests with a query string | |
# You should disable this section if you use query string only for users tracking | |
if ($query_string != "") { | |
set $no_cache 1; | |
} | |
# Disable cache for all logged in users or recent commenters | |
# Add your custom cookies here! | |
if ($http_cookie ~* "nginx_no_cache|PHPSESSID") { | |
set $skip_cache 1; | |
} | |
# Wordpress | |
if ($http_cookie ~* "comment_author|wordpress_[a-f0-9]+|wp-postpass|wordpress_no_cache|wordpress_logged_in") { | |
set $skip_cache 1; | |
} | |
# Magento | |
if ($http_cookie ~* "frontend_cid|frontend|sid|adminhtml|EXTERNAL_NO_CACHE") { | |
set $skip_cache 1; | |
} | |
# Disable cache for uris containing the following segments | |
# If is evil and you can speedup this section by using locations instead | |
# https://www.nginx.com/resources/wiki/start/topics/depth/ifisevil/ | |
# https://serverfault.com/questions/509327/can-we-jump-to-another-location-from-a-location-in-nginx | |
# Best practice | |
if ($request_uri ~* "/ping|/metrics|/nginx_status|/admin|/login|/feed|index.php|sitemap(_index)?.xml") { | |
set $skip_cache 1; | |
} | |
# Wordpress | |
if ($request_uri ~* "/wp-admin/|/xmlrpc.php|wp-.*.php|/feed/|index.php|sitemap(_index)?.xml") { | |
set $skip_cache 1; | |
} | |
# Magento | |
# I copied this from varnish example | |
# Better use varnish with magento autogenerated config instead | |
# http://devdocs.magento.com/guides/v2.0/config-guide/varnish/config-varnish.html | |
if ($request_uri ~* "(index|litespeed).php|admin|api|cron.php|/checkout/|/account/|/brand-ambassador/|/brand-ambassador-coupon/|/brand-ambassador-program/|/affiliateplusstatistic/|/brand-ambassador/index/listTransaction/|/brand-ambassador/index/paymentForm/|/brand-ambassador-program/index/index/|/brand-ambassador/banner/list/|/brand-ambassador-coupon/index/index/|/brand-ambassador/refer/index/|/brand-ambassador/index/payments/|/brand-ambassador/index/referrers/|/affiliateplusstatistic/statistic/index/|/brand-ambassador/account/edit/|/checkout/cart/|/repeat-delivery") { | |
set $skip_cache 1; | |
} | |
# Example of proxy location | |
location /backend { | |
# Disable cache for all requests except GET and HEAD | |
# And add our cookie for preventing caching for this user for next 5s | |
# So user would see the changes | |
if ($request_method !~ ^(GET|HEAD)$) { | |
set $no_cache "1"; | |
add_header Set-Cookie "nginx_no_cache=1; Max-Age=5; Path=/"; | |
add_header X-Microcachable "0"; | |
} | |
proxy_pass http://127.0.0.1:8080/; | |
proxy_cache_bypass $skip_cache; | |
proxy_no_cache $skip_cache; | |
proxy_cache PROXYCACHE; | |
proxy_cache_valid 499 502 503 1m; | |
proxy_cache_valid 404 15m; | |
proxy_cache_valid any 15m; | |
} | |
location / { | |
try_files $uri $uri/ /index.php?$args; | |
} | |
location ~ \.php$ { | |
# Disable cache for all requests except GET and HEAD | |
# And add our cookie for preventing caching for this user for next 5s | |
# So user would see the changes | |
if ($request_method !~ ^(GET|HEAD)$) { | |
set $no_cache "1"; | |
add_header Set-Cookie "nginx_no_cache=1; Max-Age=5; Path=/"; | |
add_header X-Microcachable "0"; | |
} | |
try_files $uri =404; | |
include fastcgi_params; | |
fastcgi_pass 127.0.0.1:9000; | |
fastcgi_cache_bypass $skip_cache; | |
fastcgi_no_cache $skip_cache; | |
fastcgi_cache FASTCGICACHE; | |
fastcgi_cache_valid 499 502 503 1m; | |
fastcgi_cache_valid 404 15m; | |
fastcgi_cache_valid any 15m; | |
} | |
# This would work only with external fastcgi_cache_purge module | |
# Or nginx+ commercial license | |
# Otherwise you can just delete cache location and nginx would create it from zero | |
#location ~ /purge(/.*) { | |
# fastcgi_cache_purge FASTCGICACHE "$scheme$request_method$host$1"; | |
# fastcgi_cache_purge PROXYCACHE "$scheme$request_method$host$1"; | |
#} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment