【问题标题】:nginx slow on every second requestnginx每秒请求一次慢
【发布时间】:2018-11-08 06:14:35
【问题描述】:

我目前正在为我的网站创建一个 api,使用 nodejs 和 nginx,我已经为我将运行的每个 nodejs 应用程序设置了反向代理(api、主站点、其他东西..)。

但是,当我尝试我的 api 时,它会在每秒请求一次时使用很长时间,有时会超时..

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;

}

错误日志

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

知道有什么问题吗? 这种情况已经有一段时间了,它杀了我:(

请帮忙,这浪费了我开发应用程序的时间:/

【问题讨论】:

  • 您是否尝试过绕过 NGinx 并直接连接到您的应用程序?看起来 Nginx 无法连接到上游,我猜是你的应用程序。
  • 是的,我有。我通过设置上游变量并使用它而不是localhost:2345 尝试了一些新的东西。现在一切正常! :) 我会写一个正确的答案,我会解释我是如何修复它的!

标签: node.js nginx


【解决方案1】:

添加这个是因为这对我有用

https://forum.nginx.org/read.php?15,239760,239760 似乎表明您可以 proxy_pass 到 127.0.0.1 而不是 localhost 并且请求顺利通过

麦克布雷施

一岁了,但我想指出这个问题有解决办法。就像 Cruz Fernandez 写的那样,您可以在 proxy_pass 指令上设置 127.0.0.1 而不是 localhost。这可以防止每秒请求的 60 秒延迟。我使用的是 Windows 8.1 和 nginx 1.9.5。

克鲁兹·费尔南德斯写道:

您可以使用 127.0.0.1(而不是 proxy_pass 指令上的 localhost)

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

【讨论】:

    【解决方案2】:

    我能够通过使用上游语句来解决此问题。

    例如。 :

    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;
        }
    }
    

    参考:http://nginx.org/en/docs/http/ngx_http_upstream_module.html

    【讨论】:

    • 这个问题可能是由于使用loopback接口(localhost)分别作为目标服务器和nginx的绑定目标引起的,使用真实本地地址为我解决了这个问题。跨度>
    猜你喜欢
    • 2012-06-23
    • 1970-01-01
    • 2016-03-18
    • 2018-03-14
    • 1970-01-01
    • 2020-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多