T
T
Tranz2013-11-04 01:46:24
Nginx
Tranz, 2013-11-04 01:46:24

Requests to Node through Nginx hang

Good day to all.

It became interesting to play around with the combination of Node.JS + Nginx. Wrote a test site, raised nginx, but happiness did not come.
Nginx works just as a proxy, but there were problems with css (although sometimes there is a delay in loading the page itself, and then everything works fine). The request stupidly waits a minute. If you ask the site directly, everything works fine. Only through nginx - problems.
Node renders css as static content.

image

config
#user nobody;
worker_processes 1;

#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;

#pid logs/nginx.pid;

events {
worker_connections 1024;
}

http {
include mime.types;
default_type application/octet-stream;

sendfile on;
#tcp_nopush on;

keepalive_timeout 65;

gzip on;
gzip_min_length 10240;
gzip_proxied expired no-cache no-store private auth;
gzip_types text/plain text/css text/xml text/javascript application/x-javascript application/xml;
gzip_disable «MSIE [1-6]\.»;

server {
listen 80;
server_name localhost;

#charset koi8-r;

#access_log logs/host.access.log main;

location / {
root html;
index index.html index.htm;
}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}

}

server {
listen 80;
server_name .test.com;

location / {
proxy_pass localhost:9999/;
proxy_redirect off;

proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}



location *\.(js|css|png|jpg|jpeg|gif|ico|html)$ {
expires max;
}
}
}



Thank you all in advance.

The issue was resolved by using nginx-1.2.3 instead of nginx-1.5.6

Answer the question

In order to leave comments, you need to log in

6 answer(s)
A
Anatoly, 2013-11-04
@taliban

Isn't it easier to give statics directly through nginx? What is the point of pulling a node because of pictures and styles?

R
rozhik, 2013-11-04
@rozhik

In a bunch of Nginx + node, with the correct configuration, there are no problems. Both static and dynamic.
However, if you describe how static content is served through a node, and a piece of the nginx config, then it is easier to give advice.
For example, static on a node is given by a dozen different modules, which one you use is not known.
Judging by the beautiful minute, the return occurs by timeout. The problem can be node code (which is most likely), or protocol (for example, after a minute of waiting for the thread to close, it closes automatically).

P
pomeo, 2013-11-04
@pomeo

And where did you get such an example app.use(express.static(config.root + '/static'))everywhere usually like this app.use(express.static(path.join(__dirname, 'static')))
Show app.js, it will be faster.

K
Klein Maximus, 2014-08-02
@kleinmaximus

I have the same.
When you access the node directly through localhost:3000 , the page is returned instantly, and when you go through nginx localhost , every second request freezes.

F
fAra0N25, 2014-09-30
@fAra0N25

There was the same problem on nginix 1.7.5 under Windows, in ubuntu on the default config everything was fine.
Solved by moving the url to the upstream directive

config
upstream psh_backend {
  server 127.0.0.1:4020;
  keepalive 64;
}

server {
  listen 4010;
  keepalive_timeout 10;
  / ****/
  location /api {
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_set_header X-NginX-Proxy true;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_pass http://psh_backend;
  }
}

E
eugene_pr, 2015-06-24
@eugene_pr

Solved by updating nginx to 1.8.0

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question