A
A
Al2017-02-14 01:23:35
Nginx
Al, 2017-02-14 01:23:35

Why does NGINX on a Linux server give a 502 error, while everything works without NGINX?

The application runs on express.js + socket.io + redis + mysql.
I posted the site for testing on a qa-stand (14.x ubuntu server).
Ubuntu has nginx 1.10.x.
I get a 502 Bad Gateway error. In the err log, nginx writes:

2017/02/14 00:50:31 [error] 11325#11325: *17 upstream prematurely closed connection while reading response header from upstream, client: 192.168.1.1, server: admin.mydomain.com, request: "POST /section /save HTTP/1.1", upstream: " 127.0.0.1:7103/section/save ", host: "admin.mydomain.com:7001", referrer: " admin.mydomain.com:7001/section/advertising/42 "
2017/02/14 00:50:31 [error] 11325#11325: *9 upstream prematurely closed connection while reading response header from upstream, client: 192.168.1.1, server: admin.mydomain.com, request: "GET /socket .io/?EIO=3&transport=polling&t=LevT9z1&sid=9Jg8IjXWdDWb_wZvAAAB HTTP/1.1", upstream: " 127.0.0.1:7103/socket.io/?EIO=3&transport=polling&... ", host:"admin.mydomain.com:7001", referrer: "admin.mydomain.com:7001/section/advertising/42 "

The interesting thing is that the same url ( /section/save ) but different data (post-request is the same, but the sent json can be of a different size, both larger and smaller) and everything works fine.
For fun, I installed nginx on a development machine with Windows and launched it in the cluster, which was everything like on ubunt - and everything works!
nginx.conf looks like this:
user www-data;
worker_processes 8;
pid /run/nginx.pid;

events {
  worker_connections 1024;
}

http {
  sendfile on;
  tcp_nopush on;
  tcp_nodelay on;
  keepalive_timeout 65;
  types_hash_max_size 2048;
  client_max_body_size            1024m;
  include /etc/nginx/mime.types;
  default_type application/octet-stream;

  server_tokens off;

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

  gzip on;
    	gzip_disable "msie6";
    	gzip_comp_level 5;
  gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript;

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

The site config looks like this:
upstream nodes {
    ip_hash;
    server 127.0.0.1:7101;
    server 127.0.0.1:7102;
    server 127.0.0.1:7103;
    server 127.0.0.1:7104;
    server 127.0.0.1:7105;
    server 127.0.0.1:7106;
    server 127.0.0.1:7107;
    server 127.0.0.1:7108;
}

server {
  listen *:7001;
  server_name admin.mydomain.com;
  access_log off;
  error_log /var/log/nginx/error.log;
  location / {
      auth_basic "Admin Zone";
      auth_basic_user_file /mnt/sdb1/mydomain/.htpasswd; 
    proxy_pass http://nodes;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_connect_timeout 120;
    proxy_send_timeout 120;
    proxy_read_timeout 180;
  }

  location ~* \.(jpg|jpeg|gif|png|webp|ico|css|bmp|swf|js|html|txt|ejs)$ {
    root /mnt/sdb1/mydomain/admin/www;
    expires max;
  }
}

What I did:
- increased:
proxy_connect_timeout
proxy_send_timeout
proxy_read_timeout

- added:
proxy_buffers 8 32k;
proxy_buffer_size 64k;
proxy_redirect off;

= result is null!
The application works on all ports listed in upstream, nothing falls.
Explain to me why nginx cuts the connection? What did I do wrong here?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rustamka Vorontsov, 2017-02-14
@rmfordev

Is that clear?

location / {
    try_files $uri @nodes;
}
location @nodes {
    proxy_pass http://nodes;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header Host $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    #proxy_http_version 1.1;
    #proxy_set_header Upgrade $http_upgrade;
    #proxy_set_header Connection "upgrade";
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question