andersfylling
andersfylling

Reputation: 748

nginx slow on every second request

I'm currently creating an api for my website, using nodejs and nginx, I've setup reversed proxies for each nodejs app i'll have running (api, mainsite, other stuff..).

However, when i try my api, it will use a very long time on every second request, sometimes time out..

NGINX.CONF

# For more information on configuration, see:
#   * Official English Documentation: http://nginx.org/en/docs/
#   * Official Russian Documentation: http://nginx.org/ru/docs/

user              nginx;
worker_processes  24;

error_log  /var/log/nginx/error.log;
#error_log  /var/log/nginx/error.log  notice;
#error_log  /var/log/nginx/error.log  info;

pid        /var/run/nginx.pid;


events {
    worker_connections  19000;
    multi_accept    on;
}


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

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    #SSL performance tuning
    ssl_protocols       TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers         ECDHE-RSA-AES128-SHA384:AES256-SHA256:RC4:HIGH:!MD5:!aNULL:!eNULL:!NULL:!DH:!EDH:!AESGCM;
    ssl_prefer_server_ciphers   on;
    ssl_session_cache       shared:SSL:10m;
    ssl_session_timeout     10m;

    ssl_stapling        on;
    ssl_stapling_verify     on;
    resolver            8.8.8.8 8.8.4.4 valid=300s;
    resolver_timeout        10s;
    add_header          Strict-Transport-Security "max-age=31536000";

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay             on;
    keepalive_timeout       10;

    gzip            on;
    gzip_disable        "msie6";
    gzip_min_length     1000;
    gzip_proxied        expired no-cache no-store private auth;
    gzip_types          text/plain application/xml application/javascript text/css application/x-javascript;  

    #for mulyiple domains, www.codewolf.red, codewolf.red
    server_names_hash_bucket_size 64;

    # Load config files from the /etc/nginx/conf.d directory
    # The default server is in conf.d/default.conf
    include /etc/nginx/conf.d/*.conf;

}

ERROR.LOG

2014/10/27 14:26:46 [error] 6968#8992: *15 WSARecv() failed (10054: FormatMessage() error:(317)) while reading response header from upstream, client: ::1, server: localhost, request: "GET /api/ffd/users HTTP/1.1", upstream: "http://127.0.0.1:3000/ffd/users", host: "localhost"

2014/10/27 14:27:46 [error] 6968#8992: *15 upstream timed out (10060: FormatMessage() error:(317)) while connecting to upstream, client: ::1, server: localhost, request: "GET /api/ffd/users HTTP/1.1", upstream: "http://[::1]:3000/ffd/users", host: "localhost"

2014/10/27 14:39:31 [error] 6968#8992: *20 upstream timed out (10060: FormatMessage() error:(317)) while connecting to upstream, client: ::1, server: localhost, request: "GET /api/ffd/users HTTP/1.1", upstream: "http://[::1]:3000/ffd/users", host: "localhost"
2014/10/27 14:40:09 [notice] 5300#1352: signal process started

Any idea whats wrong? It's been like this for a while, and its killing me :(

Please help, it's ruining my time for developing apps :/

Upvotes: 5

Views: 7025

Answers (2)

TylerHaigh
TylerHaigh

Reputation: 298

Adding this because this is what worked for me

https://forum.nginx.org/read.php?15,239760,239760 Seems to indicate that you can proxy_pass to 127.0.0.1 instead of localhost and the request goes through fine

macbresch

One year old but I wanted to point out that there is a work around for that issue. Like Cruz Fernandez wrote you can set 127.0.0.1 instead of localhost on the proxy_pass directive. This prevents the 60s delay on every second request. I'm using Windows 8.1 and nginx 1.9.5.

Cruz Fernandez Wrote:

you can use 127.0.0.1 (instead of localhost on the proxy_pass directive)

location /nodejsServer/ {
    proxy_pass http://127.0.0.1:3000/;
}

Upvotes: 7

andersfylling
andersfylling

Reputation: 748

I was able to fix this by using an upstream statement.

eg. :

upstream nodejs_server {
    server 192.168.0.67:8080;   #ip to nodejs server
}

#server config
server {
    location /nodejsServer/ {    #http://localhost/nodejsServer/
        proxy_pass http://nodejs_server;
    }
}

reference: http://nginx.org/en/docs/http/ngx_http_upstream_module.html

Upvotes: 3

Related Questions