Skip to content

Instantly share code, notes, and snippets.

@johnsi15
Forked from MichaelCduBois/NginxCache.conf
Created October 12, 2019 21:49
Show Gist options
  • Save johnsi15/3891ef165a17000dd925d64f9f9c07e0 to your computer and use it in GitHub Desktop.
Save johnsi15/3891ef165a17000dd925d64f9f9c07e0 to your computer and use it in GitHub Desktop.
Nginx Cache with Reverse Proxy
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] -- "$server_name$uri" -- "$request" '
'$status $body_bytes_sent "$http_referer" "$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
server {
listen 80 default_server;
server_name www.domain.com;
server_tokens off;
return 301 https://$server_name$request_uri;
}
server {
listen 443 ssl http2;
server_name www.domain.com;
expires 1y; # Here is the cache expiration
server_tokens off;
add_header X-Frame-Options "SAMEORIGIN";
ssl_certificate /etc/letsencrypt/live/www.domain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/www.domain.com/privkey.pem;
location / {
proxy_read_timeout 90;
proxy_connect_timeout 90;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://frontend:9000;
}
location /api/v1 {
proxy_read_timeout 90;
proxy_connect_timeout 90;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://backend;
}
# Here is the cache setup on static files
location ~* /.(css|js|png|jpg|jpeg|gif|ico)$ {
add_header Cache-Control "public";
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment