Last active
October 14, 2019 15:27
-
-
Save MichaelCduBois/49a9dff82654fd0af4e50d7bd271d6d1 to your computer and use it in GitHub Desktop.
Nginx Cache with Reverse Proxy
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
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