145

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/*;
}
3
  • 1
    'Connection refused' means that backend does not listen to the port 9000 or its queue is filled up. This problem is related to the backend itsef. Are you able to telnet localhost 9000? You should also check your backend and php logs.
    – Andrew
    Oct 1, 2011 at 9:17
  • Updated my post. I could not telnet to localhost 9000.
    – MacMac
    Oct 1, 2011 at 10:48
  • The same error I was facing you, This enter link description here can help you
    – Tripathi29
    Nov 2, 2015 at 13:53

8 Answers 8

71

It sounds like you haven't started and configured the backend for Nginx. Start php-fpm and add the following to nginx.conf, in the http context:

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;
    }
}
5
  • 6
    Thanks man, it worked, I didn't have php-fpm installed. Cheers.
    – MacMac
    Oct 1, 2011 at 11:21
  • 15
    You're pure genius. I can't believe 1.0000000 million guides I read about this, NOBODY mentions that you must put a "listen 127.0.0.1" to enable backend. You saved me from a nightmare!!!
    – user145046
    Nov 11, 2012 at 19:39
  • 1
    you should consider to use the unix socket. View it with netstat -l and see for /var/run/php5-fpm.sock (the config for this is normally in /etc/php5/fpm/pool.d/www.conf. fastcgi_pass unix:<socket>
    – JohannesM
    May 22, 2014 at 13:59
  • 3
    you will have listen = /var/run/php5-fpm.sock inside /etc/php5/fpm/pool.d/www.conf. but you will want listen = 9000 and ;listen = /var/run/php5-fpm.sock. if you were like me. (or else alternatively you could listen to wise hint by JohannesM. which I imagine would leave you with something like fastcgi_pass unix:/var/run/php5-fpm.sock; somewhere in your nginx.conf)
    – n611x007
    Jan 1, 2015 at 16:20
  • Having the same issue with php 7.2. What do you mean by add the file in the httpd context? Would this be an additional conf file in /etc/nginx/sites-available/ folder or what?
    – PeterKA
    Feb 1, 2018 at 14:56
83

This answer is only for those who get an error like this:

connect() failed (111: Connection refused) while connecting to upstream, client .... fastcgi://[::1]:9000

Rewrite your nginx config to use ip, not dns. For instance, 127.0.0.1 instead of localhost, or remove the ipv6 alias from /etc/hosts.

7
  • 10
    You pointed my in the right direction! I though using just listen 80 was fine (and there are lots of examples out there with that) but I didn't thought that implied both IPv4 (127.0.0.1) and IPv6 ([::1]) addresses.
    – glarrain
    Aug 12, 2014 at 0:15
  • 10
    I had to change from listen 80 default_server to listen 0.0.0.0:80.
    – givanse
    Nov 12, 2014 at 6:00
  • 1
    Can you point out why this should help?
    – kaiser
    Feb 9, 2016 at 2:38
  • 2
    Becouse most linux distributions have ipv6 enabled in networking, but not all packets configured for ipv6 use. In my opinion, when nginx initiates connection to upstream, system resolver returns ipv6 adress first. Php-fpm (centos 7.x) had no such settings from box. And most guides explain all in ipv4 version, forgetting about ipv6 futures that should be disabled or used.
    – Quake1TF
    Feb 9, 2016 at 14:54
  • 2
    Whooah, so [::1] is localhost IPv6 address! :) Thanks!
    – lechup
    Jan 4, 2017 at 11:08
11

Had the same problem with proxied requests to a Node server listening on port 5000. Requests would result with 200 OK but sometime 502 Bad Gateway randomly. NGINX showed the error:

connect() failed (111: Connection refused) while connecting to upstream, client: ..., server: ...

My solution:

  1. Set node HTTP server to listen strictly for ipv4 by including localhost as host: server.listen(5000, 'localhost');
  2. Removed any ipv6 listen directives (listen [::]:80; or listen [::]:443 ssl default_server;).
  3. Changed location block proxy_pass to use IPs: proxy_pass http://127.0.0.1:5000 (not proxy_pass http://localhost:5000).

Hope this helps someone.

1
  • Do you know why this helped you?
    – Manuel
    Oct 22, 2022 at 16:07
10

Got errors like this too. Problem was my abstract backend referencing two servers. php-fpm was only listing to socket...

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

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).

1

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:

access_log syslog:server=10.0.1.48:514,facility=local4,tag=nginx,severity=debug,nohostname main;

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

1

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).

-3

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .