skip to Main Content

Every time when I’m restart the upstream server, my NGINX shows "bad gateway" which is ok, but later, when the upstream server restarts nginx not recover automatically and I need to restart it (the nginx) manually.

Is there an option to make nginx to check every few seconds if the upstream backed to normal?

    upstream core {

    server core:3001;
}

server {
    server_name core.mydomain.com corestg.mydomain.com www.core.mydomain.com;

    #listen 80;
    #listen [::]:80;

    gzip on;
    gzip_static on;    
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript;
    gzip_proxied  any;
    #gzip_vary on;
    gzip_comp_level 6;
    gzip_buffers 16 8k;
    gzip_http_version 1.1;    
     listen 443 ssl http2;
     listen [::]:443 ssl http2;

    resolver 8.8.8.8 8.8.4.4 valid=300s;
    resolver_timeout 5s;

    server_tokens off;
    ssl_certificate /etc/ssl/domain.crt;
    ssl_certificate_key /etc/ssl/domain.rsa; 

    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;

    location / {
        proxy_ssl_session_reuse off;
        proxy_pass http://core;
        proxy_buffers 8 24k;
        proxy_buffer_size 2k;
        proxy_http_version 1.1;
        proxy_ignore_headers X-Accel-Expires Expires Cache-Control;
        proxy_ignore_headers Set-Cookie;
        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 https;
        proxy_set_header X-NginX-Proxy true;
#       proxy_set_header Host $http_host;
        proxy_set_header Host $host;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_cache_bypass $http_upgrade;
        proxy_redirect off;
    }
}

2

Answers


  1. Chosen as BEST ANSWER

    Seems that NGINX does not do the auto recovery by default. Changing the config part from:

    upstream core {
        server core:3001;
    }
    

    to:

    {
        server core:3001  max_fails=1 fail_timeout=1s;
        server core:3001  max_fails=1 fail_timeout=1s;
    }
    

    did the trick. the duplication is not mistake. Nginx tries to resolve the first line, on failure it will try the second one (circularly).


  2. My setup to test NGINX:

    Docker-Container simulating the backend exposing port 9002.

    afd9551abc54   nginx            "/docker-entrypoint.…"   About a minute ago   Up 11 seconds   0.0.0.0:9002->80/tcp                          laughing_pike
    

    NGINX configuration

    # Defined upstream block.
    upstream backend {
       server 127.0.0.1:9002;
    }
    
    #Main Server block
    server {
      listen 80;
    
      location / {
        proxy_pass       http://backend;
        proxy_set_header Host $host;
      }
    
    }
    

    Stopping the container will result in 502 Bad Gateway. Starting the container without restarting / reloading NGINX sends the data to the upstream server. So basically that should just work!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search