Fix nginx configuration syntax errors
- Move proxy_set_header directives inside location blocks - Add DNS resolver for dynamic upstream resolution - Improve fallback configuration in docker-compose - Add config validation before starting nginx This should resolve the nginx startup failures.
This commit is contained in:
@@ -31,8 +31,11 @@ services:
|
|||||||
# Remove default nginx configuration files to prevent conflicts
|
# Remove default nginx configuration files to prevent conflicts
|
||||||
rm -rf /etc/nginx/conf.d/*
|
rm -rf /etc/nginx/conf.d/*
|
||||||
|
|
||||||
if [ ! -f /etc/nginx/nginx.conf ]; then
|
# Test the main config first
|
||||||
echo 'Creating fallback nginx configuration...'
|
if nginx -t -c /etc/nginx/nginx.conf 2>/dev/null; then
|
||||||
|
echo 'Using main nginx configuration'
|
||||||
|
else
|
||||||
|
echo 'Main config failed, creating fallback nginx configuration...'
|
||||||
cat > /etc/nginx/nginx.conf << 'EOF'
|
cat > /etc/nginx/nginx.conf << 'EOF'
|
||||||
events {
|
events {
|
||||||
worker_connections 1024;
|
worker_connections 1024;
|
||||||
@@ -56,6 +59,9 @@ services:
|
|||||||
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_connect_timeout 5s;
|
||||||
|
proxy_send_timeout 60s;
|
||||||
|
proxy_read_timeout 60s;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -8,28 +8,15 @@ http {
|
|||||||
server portfolio-app-1:3000 max_fails=3 fail_timeout=30s;
|
server portfolio-app-1:3000 max_fails=3 fail_timeout=30s;
|
||||||
server portfolio-app-2:3000 max_fails=3 fail_timeout=30s;
|
server portfolio-app-2:3000 max_fails=3 fail_timeout=30s;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Resolver for dynamic upstream resolution
|
||||||
|
resolver 127.0.0.11 valid=10s;
|
||||||
|
|
||||||
# Main server
|
# Main server
|
||||||
server {
|
server {
|
||||||
listen 80;
|
listen 80;
|
||||||
server_name _;
|
server_name _;
|
||||||
|
|
||||||
# Proxy settings
|
|
||||||
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;
|
|
||||||
|
|
||||||
# Timeout settings
|
|
||||||
proxy_connect_timeout 5s;
|
|
||||||
proxy_send_timeout 60s;
|
|
||||||
proxy_read_timeout 60s;
|
|
||||||
|
|
||||||
# Buffer settings
|
|
||||||
proxy_buffering on;
|
|
||||||
proxy_buffer_size 4k;
|
|
||||||
proxy_buffers 8 4k;
|
|
||||||
|
|
||||||
# Health check endpoint
|
# Health check endpoint
|
||||||
location /health {
|
location /health {
|
||||||
access_log off;
|
access_log off;
|
||||||
@@ -41,6 +28,22 @@ http {
|
|||||||
location / {
|
location / {
|
||||||
proxy_pass http://portfolio_backend;
|
proxy_pass http://portfolio_backend;
|
||||||
|
|
||||||
|
# Proxy settings
|
||||||
|
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;
|
||||||
|
|
||||||
|
# Timeout settings
|
||||||
|
proxy_connect_timeout 5s;
|
||||||
|
proxy_send_timeout 60s;
|
||||||
|
proxy_read_timeout 60s;
|
||||||
|
|
||||||
|
# Buffer settings
|
||||||
|
proxy_buffering on;
|
||||||
|
proxy_buffer_size 4k;
|
||||||
|
proxy_buffers 8 4k;
|
||||||
|
|
||||||
# Health check for upstream
|
# Health check for upstream
|
||||||
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
|
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
|
||||||
proxy_next_upstream_tries 2;
|
proxy_next_upstream_tries 2;
|
||||||
@@ -50,6 +53,13 @@ http {
|
|||||||
# Static files caching
|
# Static files caching
|
||||||
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ {
|
location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ {
|
||||||
proxy_pass http://portfolio_backend;
|
proxy_pass http://portfolio_backend;
|
||||||
|
|
||||||
|
# Proxy settings
|
||||||
|
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;
|
||||||
|
|
||||||
expires 1y;
|
expires 1y;
|
||||||
add_header Cache-Control "public, immutable";
|
add_header Cache-Control "public, immutable";
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user