Skip to content

Instantly share code, notes, and snippets.

@Codes-Lab
Created May 18, 2023 13:46
Show Gist options
  • Save Codes-Lab/d5b6d45ac3b15be9c6575e4476fa2d70 to your computer and use it in GitHub Desktop.
Save Codes-Lab/d5b6d45ac3b15be9c6575e4476fa2d70 to your computer and use it in GitHub Desktop.
nginx.conf with proxy configuration and proxy caching configuration. It also logs the status of cache if there was a cache hit or cache miss in the host.access.log
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" $upstream_cache_status [$time_local]' ;
access_log logs/access.log main;
keepalive_timeout 65;
proxy_cache_path /temp/cache keys_zone=mycache:10m;
server {
listen 80;
server_name localhost;
access_log logs/host.access.log main;
proxy_cache mycache;
location / {
proxy_pass http://example.com/;
# Calculating the cache key
proxy_cache_key "$host$request_uri$cookie_user";
# Cache the responses to requests with methods
proxy_cache_methods GET HEAD POST;
# Reponses with status codes 200 and 206 are cached for 1 hour.
proxy_cache_valid 200 206 1h;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment