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

Mac*_*Mac 117 nginx 502-error

502 Gateway在访问目录 ( http://example.com/dev/index.php) 中的 PHP 文件时遇到错误。日志只是这样说:

2011/09/30 23:47:54 [error] 31160#0: *35 connect() failed (111: Connection refused) while connecting to upstream, client: xx.xx.xx.xx, server: domain.com, request: "GET /dev/ HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "domain.com"
Run Code Online (Sandbox Code Playgroud)

我以前从未经历过这种情况。此类502 Gateway错误的解决方案是什么?

这是nginx.conf

user www-data;
worker_processes 4;
pid /var/run/nginx.pid;

events {
    worker_connections 768;
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

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

    ##
    # Logging Settings
    ##

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

    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_disable "msie6";

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # gzip_http_version 1.1;
    # gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

    ##
    # Virtual Host Configs
    ##

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}
Run Code Online (Sandbox Code Playgroud)

Qua*_*1TF 66

这个答案只适用于那些遇到这样的错误的人:

connect() 连接到上游时失败(111:连接被拒绝),客户端 .... fastcgi://[::1]:9000

重写您的 nginx 配置以使用 ip,而不是 dns。例如,127.0.0.1代替localhost,或从 /etc/hosts 中删除 ipv6 别名。

  • 我不得不从`listen 80 default_server` 改为`listen 0.0.0.0:80`。 (8认同)
  • 你为我指明了正确的方向!我虽然只使用`listen 80`很好(并且有很多例子)但我不认为这意味着IPv4(`127.0.0.1`)和IPv6(`[::1]`)地址。 (5认同)
  • 因为大多数 Linux 发行版都在网络中启用了 ipv6,但并非所有数据包都配置为使用 ipv6。在我看来,当 nginx 发起与上游的连接时,系统解析器首先返回 ipv6 地址。Php-fpm (centos 7.x) 没有这样的设置。大多数指南都以 ipv4 版本解释所有内容,忘记了应该禁用或使用的 ipv6 futures。 (2认同)
  • 哇啊,所以 [::1] 是本地主机 IPv6 地址!:) 谢谢! (2认同)

qua*_*nta 60

听起来您还没有为 Nginx 启动和配置后端。启动php-fpm并将以下内容添加到nginx.conf, 在http上下文中:

server {
    listen 127.0.0.1;
    server_name localhost;

    error_log /var/log/nginx/localhost.error_log info;

    root /var/www/localhost/htdocs;

    location ~ \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        include /etc/nginx/conf.d/*.conf;
        include /etc/nginx/sites-enabled/*;

        fastcgi_intercept_errors        on;
        error_page 404 /error/404.php;
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 你是纯粹的天才。我无法相信我读到的 1.0000000 百万个指南,没有人提到您必须放置“listen 127.0.0.1”才能启用后端。**你把我从噩梦中救了出来!!!** (9认同)
  • 谢谢伙计,它起作用了,我没有安装`php-fpm`。干杯。 (3认同)
  • 你将在 `/etc/php5/fpm/pool.d/www.conf` 中有 `listen = /var/run/php5-fpm.sock`。但是你会想要`listen = 9000`和`;listen = /var/run/php5-fpm.sock`。如果你像我一样。(或者你可以听听 JohannesM 的明智提示。我想这会让你在你的 `nginx.conf` 中的某个地方留下类似 `fastcgi_pass unix:/var/run/php5-fpm.sock;` 的东西) (2认同)

小智 8

也有这样的错误。问题是我的抽象后端引用了两台服务器。 php-fpm只列出套接字...

# Upstream to abstract backend connection(s) for php
upstream php {
        server unix:/var/run/php5-fpm.sock;
        #server 127.0.0.1:9000;
} 

server {
    [...]

    location ~ \.php$ {
            # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini

            # With php5-fpm:
            fastcgi_pass php;
            fastcgi_index index.php;
            fastcgi_intercept_errors on;
            include fastcgi_params;
    }
}
Run Code Online (Sandbox Code Playgroud)


小智 6

对侦听端口 5000 的节点服务器的代理请求也有同样的问题。请求会产生200 OK但有时是502 Bad Gateway随机的。NGINX 显示错误:

connect() failed (111: Connection refused) while connecting to upstream, client: ..., server: ...
Run Code Online (Sandbox Code Playgroud)

我的解决方案:

  1. 通过将 localhost 作为主机,将节点 HTTP 服务器设置为严格侦听ipv4:server.listen(5000, 'localhost');
  2. 删除了任何 ipv6 监听指令(listen [::]:80;listen [::]:443 ssl default_server;)。
  3. 将位置块 proxy_pass 更改为使用 IP:(proxy_pass http://127.0.0.1:5000不是proxy_pass http://localhost:5000)。

希望这可以帮助某人。