Trust NPM proxy subnets, extract real client IP from X-Forwarded-For, log with referrer and user agent. Authored by: Jack Levy
69 lines
2.3 KiB
Nginx Configuration File
69 lines
2.3 KiB
Nginx Configuration File
events {
|
|
worker_connections 1024;
|
|
}
|
|
|
|
http {
|
|
include /etc/nginx/mime.types;
|
|
default_type application/octet-stream;
|
|
sendfile on;
|
|
keepalive_timeout 65;
|
|
|
|
# Trust NPM as a proxy — replace $remote_addr with the real client IP
|
|
# from X-Forwarded-For when the request comes from NPM's subnet
|
|
real_ip_header X-Forwarded-For;
|
|
real_ip_recursive on;
|
|
set_real_ip_from 192.168.0.0/16;
|
|
set_real_ip_from 10.0.0.0/8;
|
|
set_real_ip_from 172.16.0.0/12;
|
|
|
|
# Log format includes real IP, referrer, and user agent
|
|
log_format main '$remote_addr - [$time_local] "$request" '
|
|
'$status $body_bytes_sent "$http_referer" '
|
|
'"$http_user_agent"';
|
|
access_log /var/log/nginx/access.log main;
|
|
|
|
# Use Docker's internal DNS; valid=10s forces re-resolution after container restarts.
|
|
# Variables in proxy_pass activate this resolver (upstream blocks do not).
|
|
resolver 127.0.0.11 valid=10s ipv6=off;
|
|
|
|
server {
|
|
listen 80;
|
|
server_name _;
|
|
|
|
client_max_body_size 10M;
|
|
|
|
# API — variable forces re-resolution via resolver on each request cycle
|
|
location /api/ {
|
|
set $api http://api:8000;
|
|
proxy_pass $api;
|
|
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_read_timeout 120s;
|
|
proxy_connect_timeout 10s;
|
|
}
|
|
|
|
# Next.js static assets (long cache)
|
|
location /_next/static/ {
|
|
set $frontend http://frontend:3000;
|
|
proxy_pass $frontend;
|
|
proxy_cache_valid 200 1d;
|
|
add_header Cache-Control "public, max-age=86400, immutable";
|
|
}
|
|
|
|
# Everything else → frontend
|
|
location / {
|
|
set $frontend http://frontend:3000;
|
|
proxy_pass $frontend;
|
|
proxy_set_header Host $host;
|
|
proxy_set_header X-Real-IP $remote_addr;
|
|
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
|
# WebSocket support (Next.js HMR in dev)
|
|
proxy_http_version 1.1;
|
|
proxy_set_header Upgrade $http_upgrade;
|
|
proxy_set_header Connection "upgrade";
|
|
}
|
|
}
|
|
}
|