Skip to content

Nginx

nginx config(/etc/nginx/nginx.conf) to forward req to upstream server

# Upstream block: defines a pool of backend servers
upstream my_backend {
    server 192.108.1.1:3002;  # First backend server
    server 192.108.1.2:3002;  # Second backend server
    server 192.108.1.3:3002;  # Third backend server
}

# Main server block: handles incoming requests on port 80
server {
    listen 80;  # Listen on HTTP port 80
    listen [::]:80;  # Listen on HTTP port 80 for IPV6
    server_name www.example.com;  # The domain name this server block responds to

    location / {
        proxy_pass http://my_backend;  # Forward requests to the upstream group
        proxy_connect_timeout 5s;  # Timeout for establishing a connection to the upstream
        proxy_send_timeout 5s;  # Timeout for sending a request to the upstream
        proxy_read_timeout 5s;  # Timeout for reading a response from the upstream

        # Forward the original host header (domain)
        proxy_set_header Host $host;  
        # Forward the real client IP address
        proxy_set_header X-Real-IP $remote_addr;  
        # Forward the chain of client IPs (if behind multiple proxies)
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;  
        # Forward any additional custom headers, like auth tokens (example)
        proxy_set_header X-Auth-Token $http_x_auth_token;  # Pass custom auth header if present

        # Retry and failover conditions
        proxy_next_upstream error timeout http_502 http_503 http_504;  # Retry on these specific errors
    }
}
graph LR
    ClientRequest --> Nginx
    Nginx -->|With Forward Headers| UpstreamServer1
    Nginx -->|With Forward Headers| UpstreamServer2
    Nginx -->|With Forward Headers| UpstreamServer3
    UpstreamServer1 --> Nginx
    UpstreamServer2 --> Nginx
    UpstreamServer3 --> Nginx
    Nginx --> ClientRequest