I'm experiencing 502 Gateway
errors when accessing a PHP file in a directory (http://example.com/dev/index.php
). The logs simply says this:
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"
I've never experienced this before. What is the solution for this type of 502 Gateway
error?
This is the 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/*;
}
This answer is only for those who get an error like this:
Rewrite your nginx config to use ip, not dns. For instance,
127.0.0.1
instead oflocalhost
, or remove the ipv6 alias from /etc/hosts.It sounds like you haven't started and configured the backend for Nginx. Start
php-fpm
and add the following tonginx.conf
, in thehttp
context:Got errors like this too. Problem was my abstract backend referencing two servers.
php-fpm
was only listing to socket...Had the same problem with proxied requests to a Node server listening on port 5000. Requests would result with
200 OK
but sometime502 Bad Gateway
randomly. NGINX showed the error:My solution:
server.listen(5000, 'localhost');
listen [::]:80;
orlisten [::]:443 ssl default_server;
).proxy_pass http://127.0.0.1:5000
(notproxy_pass http://localhost:5000
).Hope this helps someone.
In my case the error was a bad location for the error_log file for php5.6-fpm service and thus the php-fpm service was failing to start and nginx was not able to connect to it. You can find it in
/etc/php/5.6/fpm/php.ini
(you can replace 5.6 with the version you are running).Just in case somebody is deperately trying to fix their problem just to realize there is nothing wrong with their reverse proxy setup:
In my case, the error persisted even after I've removed all
location
directives but a single one that only provides static content.The error message was caused because Nginx wasn't able to log it's log to the syslog server:
Summary:
If you are using a syslog log server, make sure it is available. To test whether the error originates from the logging setup, comment out all logging configs so that Nginx falls back to the native logging scheme.
I hope this saves some people time debugging a fully valid reverse proxy config, just to fid the error somewhere else :D
Same problem has occured for me and finally I found firewalld was blocking required ports after installation and I was missing to open ports in firewall (port 9000 in your logs).
Just today I ran into this problem and for me it was a low memory issue during a high load period. So leveling up the instance type fixed the problem.