502 Bad Gateway on Nginx

ServerAmateur asked:

We currently have a web app built to run on an angular framework on Nginx running on Ubuntu 1.10.3. The error log for an attempt to access the site is:

30364#30364: 1000 worker_connections are not enough
30364#30364: *5179370 recv() failed (104: Connection reset by peer) while 
reading response header from upstream server:mysite request: "GET / 
HTTP/1.1" upstream: myip:80 host:mysite
30364#30364: 1000 worker_connections are not enough
30364#30364: *5179454 no live upstreams while connecting to upstream

My Nginx.conf:

user www-data;
worker_processes auto;
pid /run/nginx.pid;

events {
        worker_connections 1000;
}

http{
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;ssl_protocols TLSv1 TLSv1.1 
    TLSv1.2; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;

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

    gzip on;
    gzip_disable "msie6";

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

My default.conf:

server {
       listen 80 default_server;
       listen [::]:80 default_server;

       listen 443 ssl; 
       ssl_certificate /etc/letsencrypt/live/mysite/fullchain.pem; 
       ssl_certificate_key /etc/letsencrypt/live/mysite/privkey.pem; 

       root /home/admin_user/myroot;

       large_client_header_buffers 4 32k;
       index index.html index.htm index.nginx-debian.html;

       server_name myserver;

       add_header Strict-Transport-Security max-age=500;

     location / {
            proxy_pass http://mysite;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for ; 
            proxy_set_header X-Forwarded-Proto https; 
      }
}

The file structure of my root directory is along these lines.
Dashes denote levels.
-root
–app
—controllers models services styles views
–bower_components
–config
–data
–data.xlsx
–file.jpg
–index.html
–node_modules
–package.json
–Procfile
–public
—app.js img
–server.js

This by all means is not a full view of the file structure, but I thought it may be helpful.

My answer:


You’ve configured nginx in such a way that it is attempting to connect to itself as the upstream, rather than to Node.js.

     location / {
            proxy_pass http://mysite;

Where mysite resolves to the same host.

In this scenario, nginx repeatedly connects back to itself trying to serve the request, does so 1000 times, and runs out of workers.

You have two possibilities to solve the problem:

  1. Define an upstream named mysite which references your running Node.js server. For example:

    upstream mysite {
        server [::1]:3000;
    }
    
  2. Specify the upstream URL directly in proxy_pass:

            proxy_pass http://[::1]:3000;
    

View the full question and answer on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.