fix: improve proxy config

This commit is contained in:
tigattack
2025-09-22 22:45:12 +01:00
parent bf2ea908f4
commit c3aa5534f3
2 changed files with 22 additions and 1 deletions

View File

@@ -239,7 +239,24 @@ const PORT = process.env.PORT || 3001;
// Trust proxy (needed when behind reverse proxy) and remove X-Powered-By // Trust proxy (needed when behind reverse proxy) and remove X-Powered-By
if (process.env.TRUST_PROXY) { if (process.env.TRUST_PROXY) {
app.set('trust proxy', process.env.TRUST_PROXY === 'true' ? 1 : parseInt(process.env.TRUST_PROXY, 10) || true); const trustProxyValue = process.env.TRUST_PROXY;
// Parse the trust proxy setting according to Express documentation
if (trustProxyValue === 'true') {
app.set('trust proxy', true);
} else if (trustProxyValue === 'false') {
app.set('trust proxy', false);
} else if (/^\d+$/.test(trustProxyValue)) {
// If it's a number (hop count)
app.set('trust proxy', parseInt(trustProxyValue, 10));
} else {
// If it contains commas, split into array; otherwise use as single value
// This handles: IP addresses, subnets, named subnets (loopback, linklocal, uniquelocal)
app.set('trust proxy', trustProxyValue.includes(',')
? trustProxyValue.split(',').map(s => s.trim())
: trustProxyValue
);
}
} else { } else {
app.set('trust proxy', 1); app.set('trust proxy', 1);
} }

View File

@@ -36,6 +36,10 @@ server {
proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme; proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Host $host;
# Preserve original client IP through proxy chain
proxy_set_header X-Original-Forwarded-For $http_x_forwarded_for;
# CORS headers for API calls # CORS headers for API calls
add_header Access-Control-Allow-Origin * always; add_header Access-Control-Allow-Origin * always;