Files
OpenFrontIO/nginx.conf
T
evanpelle ea628570b2 have nginx cache index for 1s. (#224)
This is required for A B deployments, the browser wasn't updating due to
conditional caches
2025-03-12 11:03:28 -07:00

252 lines
9.3 KiB
Nginx Configuration File

# Map URI to ports
map $uri $port {
~^/w0/ 3001;
~^/w1/ 3002;
~^/w2/ 3003;
~^/w3/ 3004;
~^/w4/ 3005;
~^/w5/ 3006;
~^/w6/ 3007;
~^/w7/ 3008;
~^/w8/ 3009;
~^/w9/ 3010;
~^/w10/ 3011;
~^/w11/ 3012;
~^/w12/ 3013;
~^/w13/ 3014;
~^/w14/ 3015;
default 3000;
}
# WebSocket settings
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
# WebSocket path handling
map $uri $uri_path {
~^/w\d+(/.*)?$ $1;
default $uri;
}
# Cache configuration
proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=STATIC:10m inactive=24h max_size=1g;
# API cache for frequently requested endpoints
proxy_cache_path /var/cache/nginx/api levels=1:2 keys_zone=API_CACHE:10m inactive=60m max_size=100m;
server {
listen 80 default_server;
# Logging
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
# Static file handling with proper MIME types and consistent caching
location ~* ^/static/(.*)$ {
proxy_pass http://127.0.0.1:3000;
# Include MIME types
include /etc/nginx/mime.types;
# Cache configuration for static files
proxy_cache STATIC;
proxy_cache_valid 200 302 24h; # Cache successful responses for 24 hours
proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
proxy_cache_lock on;
# Show cache status in response headers
add_header X-Cache-Status $upstream_cache_status;
# Standard proxy headers
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;
# Default cache policy for static files
add_header Cache-Control "public, max-age=86400"; # 24 hours
}
# /api/public_lobbies endpoint - Cache for 1 second to handle high request volume
location = /api/public_lobbies {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
# Cache configuration
proxy_cache API_CACHE;
proxy_cache_valid 200 1s;
proxy_cache_use_stale updating error timeout http_500 http_502 http_503 http_504;
proxy_cache_lock on;
add_header X-Cache-Status $upstream_cache_status;
# Standard proxy headers
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;
}
# /api/env endpoint - Cache for 1 hour
location = /api/env {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
# Cache configuration
proxy_cache API_CACHE;
proxy_cache_valid 200 1h; # Cache successful responses for 1 hour
proxy_cache_use_stale error timeout http_500 http_502 http_503 http_504;
proxy_cache_lock on;
add_header X-Cache-Status $upstream_cache_status;
# Standard proxy headers
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;
}
# Binary files caching
location ~* \.(bin|dat|exe|dll|so|dylib)$ {
proxy_pass http://127.0.0.1:3000;
add_header Cache-Control "public, max-age=31536000, immutable"; # 1 year for binary files
proxy_cache STATIC;
proxy_cache_valid 200 302 24h;
proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
proxy_cache_lock on;
add_header X-Cache-Status $upstream_cache_status;
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;
}
# Specific file type caching rules (outside the /static/ location)
location ~* \.js$ {
proxy_pass http://127.0.0.1:3000;
add_header Content-Type application/javascript;
add_header Cache-Control "public, max-age=31536000, immutable"; # 1 year for JS files
proxy_cache STATIC;
proxy_cache_valid 200 302 24h;
proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
proxy_cache_lock on;
add_header X-Cache-Status $upstream_cache_status;
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;
}
location ~* \.css$ {
proxy_pass http://127.0.0.1:3000;
add_header Content-Type text/css;
add_header Cache-Control "public, max-age=31536000, immutable"; # 1 year for CSS files
proxy_cache STATIC;
proxy_cache_valid 200 302 24h;
proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
proxy_cache_lock on;
add_header X-Cache-Status $upstream_cache_status;
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;
}
# Updated HTML file caching - 1 second cache
location ~* \.html$ {
proxy_pass http://127.0.0.1:3000;
add_header Content-Type text/html;
add_header Cache-Control "public, max-age=1"; # 1 second for HTML files
proxy_cache STATIC;
proxy_cache_valid 200 302 1s; # Cache successful responses for 1 second
proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
proxy_cache_lock on;
add_header X-Cache-Status $upstream_cache_status;
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;
}
# Updated Root location - with 1 second cache
location = / {
proxy_pass http://127.0.0.1:3000;
add_header Cache-Control "public, max-age=1"; # 1 second cache
proxy_cache STATIC;
proxy_cache_valid 200 302 1s; # Cache successful responses for 1 second
proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
proxy_cache_lock on;
add_header X-Cache-Status $upstream_cache_status;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
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;
}
# Main location with conditional caching for index.html
location / {
proxy_pass http://127.0.0.1:3000;
# Add caching for any index.html that might be served here
if ($request_uri ~* "index\.html$") {
add_header Cache-Control "public, max-age=1";
proxy_cache STATIC;
proxy_cache_valid 200 302 1s;
proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
proxy_cache_lock on;
add_header X-Cache-Status $upstream_cache_status;
}
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
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;
}
# Worker locations
location ~* ^/w(\d+)(/.*)?$ {
set $worker $1;
set $worker_port 3001;
if ($worker = "0") { set $worker_port 3001; }
if ($worker = "1") { set $worker_port 3002; }
if ($worker = "2") { set $worker_port 3003; }
if ($worker = "3") { set $worker_port 3004; }
if ($worker = "4") { set $worker_port 3005; }
if ($worker = "5") { set $worker_port 3006; }
if ($worker = "6") { set $worker_port 3007; }
if ($worker = "7") { set $worker_port 3008; }
if ($worker = "8") { set $worker_port 3009; }
if ($worker = "9") { set $worker_port 3010; }
if ($worker = "10") { set $worker_port 3011; }
if ($worker = "11") { set $worker_port 3012; }
if ($worker = "12") { set $worker_port 3013; }
if ($worker = "13") { set $worker_port 3014; }
if ($worker = "14") { set $worker_port 3015; }
proxy_pass http://127.0.0.1:$worker_port$2;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
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;
}
}