Nginx有时“连接到上游时connect()失败(111:连接被拒绝)”

时间:2019-11-18 04:18:23

标签: node.js nginx nginx-reverse-proxy nginx-config

我在node js上使用了nginx,效果很好。但问题有时我无法访问某些路线。并显示错误connect()失败(111:连接被拒绝)并再次工作。 idk为什么多数民众赞成在。这是error.log

2019/11/18 04:04:17 [error] 9234#0: *38237 connect() failed (111: Connection refused) while connecting to upstream, client: 180.246.173.22, server: loganganteng.site, request: "GET /timeline/loadEditorChoice HTTP/2.0", upstream: "http://172.20.0.2:3000/timeline/loadEditorChoice", host: "loganganteng.site"
2019/11/18 04:04:17 [error] 9234#0: *38237 connect() failed (111: Connection refused) while connecting to upstream, client: 180.246.173.22, server: loganganteng.site, request: "GET /timeline/loadLatestPopular HTTP/2.0", upstream: "http://172.20.0.2:3000/timeline/loadLatestPopular", host: "loganganteng.site"
2019/11/18 04:04:17 [error] 9234#0: *38237 connect() failed (111: Connection refused) while connecting to upstream, client: 180.246.173.22, server: loganganteng.site, request: "GET /timeline/loadLatestPost HTTP/2.0", upstream: "http://172.20.0.2:3000/timeline/loadLatestPost", host: "loganganteng.site"

这是access.log

180.246.173.22 - - [18/Nov/2019:04:04:17 +0000] "GET /timeline/loadEditorChoice HTTP/2.0" 502 3693 "-" "okhttp/3.12.0" "-"
180.246.173.22 - - [18/Nov/2019:04:04:17 +0000] "GET /timeline/loadLatestPopular HTTP/2.0" 502 3693 "-" "okhttp/3.12.0" "-"
180.246.173.22 - - [18/Nov/2019:04:04:17 +0000] "GET /timeline/loadLatestPost HTTP/2.0" 502 3693 "-" "okhttp/3.12.0" "-"

错误并非总是如此。但是有时候我也把Nginx配置了

     upstream socket_nodes {
    ip_hash;
    server 172.20.0.2:3000;
 }

 server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    #listen 80;
    #listen [::]:80;
    server_name  loganganteng.site;
    #root         /usr/share/nginx/html;

    ssl_certificate /etc/letsencrypt/live/loganganteng.site/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/loganganteng.site/privkey.pem;
    ssl_session_timeout 1d;
    ssl_session_cache shared:SSL:50m;
    ssl_session_tickets off;

    # modern configuration. tweak to your needs.
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    ssl_ciphers 'ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:ECDHE-RSA-DES-CBC3-SHA:ECDHE-ECDSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:DES-CBC3-SHA:HIGH:SEED:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!RSAPSK:!aDH:!aECDH:!EDH-DSS-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA:!SRP';
    ssl_prefer_server_ciphers on;

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

    ssl_stapling on;
    ssl_stapling_verify on;

    resolver 8.8.8.8;
    proxy_set_header Host $http_host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    location /socket.io/ {
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_http_version 1.1;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_pass http://socket_nodes;
    }

    location / {
        proxy_pass "http://172.20.0.2:3000/";
    }

    error_page 404 /404.html;
        location = /40x.html {
    }

    error_page 500 502 503 504 /50x.html;
        location = /50x.html {
    }

}

此默认配置

    user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
worker_rlimit_nofile 20000;

include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 20000;
}

http {
    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;

    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;

    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80 default_server;
        listen       [::]:80 default_server;
        server_name  _;
        root         /usr/share/nginx/html;

        include /etc/nginx/default.d/*.conf;

        location / {
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }

1 个答案:

答案 0 :(得分:0)

可能是端口问题。使用此命令sudo lsof -i -P -n | grep LISTEN并检查所需的端口是否正在运行。 如果不是,请检查日志中是否有错误(例如:pm2 log)。