我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 别名。
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)
小智 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)
我的解决方案:
server.listen(5000, 'localhost'); listen [::]:80;或listen
[::]:443 ssl default_server;)。proxy_pass http://127.0.0.1:5000不是proxy_pass http://localhost:5000)。希望这可以帮助某人。