82

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/*;
}
  • 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 '11 at 9:17
  • Updated my post. I could not telnet to localhost 9000. – MacMac Oct 1 '11 at 10:48
  • The same error I was facing you, This enter link description here can help you – Tripathi29 Nov 2 '15 at 13:53
43

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;
    }
}
  • 3
    Thanks man, it worked, I didn't have php-fpm installed. Cheers. – MacMac Oct 1 '11 at 11:21
  • 6
    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 '12 at 19:39
  • 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 '14 at 13:59
  • 2
    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 '15 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 '18 at 14:56
45

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.

  • 3
    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 '14 at 0:15
  • 5
    I had to change from listen 80 default_server to listen 0.0.0.0:80. – givanse Nov 12 '14 at 6:00
  • Can you point out why this should help? – kaiser Feb 9 '16 at 2:38
  • 1
    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 '16 at 14:54
  • Whooah, so [::1] is localhost IPv6 address! :) Thanks! – lechup Jan 4 '17 at 11:08
4

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

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.

0

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

-4

I had the same problem and adding the listen statement

listen 127.0.0.1;

worked for me.

Interestingly enough I have other server blocks that are running quite happily without this!

  • 3
    This has been answered and accepted already, three years ago. – Sven Nov 26 '14 at 23:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.