Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I'm experiencing 502 gateway errors when accessing a PHP file in a directory (http://domain.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://", host: "domain.com"

I've never experienced this before, how do I do a 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/*;

#mail {
#   # See sample authentication script at:
#   # http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
#   # auth_http localhost/auth.php;
#   # pop3_capabilities "TOP" "USER";
#   # imap_capabilities "IMAP4rev1" "UIDPLUS";
#   server {
#       listen     localhost:110;
#       protocol   pop3;
#       proxy      on;
#   }
#   server {
#       listen     localhost:143;
#       protocol   imap;
#       proxy      on;
#   }
share|improve this question
What does nginx.conf look like? –  quanta Oct 1 '11 at 1:43
'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
I have the same issue going on. Except I'm using RAILS, so do I still need php-fpm? –  Shamoon Oct 26 '12 at 15:10

4 Answers 4

up vote 13 down vote accepted

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

server {
    server_name localhost;

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

    root /var/www/localhost/htdocs;

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

        fastcgi_intercept_errors        on;
        error_page 404 /error/404.php;
share|improve this answer
Thanks man, it worked, I didn't have php-fpm installed. Cheers. –  MacMac Oct 1 '11 at 11:21
You're pure genius. I can't believe 1.0000000 million guides I read about this, NOBODY mentions that you must put a "listen" 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 at 13:59

This answer only for those, who get error like

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

and come from second link in google search =)

Rewrite your nginx config to use ip, not dns (like instead 'localhost') or remove ipv6 alias from /etc/hosts.

share|improve this answer
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 ( and IPv6 ([::1]) addresses. –  glarrain Aug 12 at 0:15
thanks this solved my issue! –  lakshmipathi Sep 11 at 15:25
I had to change from listen 80 default_server to listen –  givanse Nov 12 at 6:00

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 {

    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;
share|improve this answer

I had the same problem and adding the listen statement


worked for me.

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

share|improve this answer
This has been answered and accepted already, three years ago. –  Sven Nov 26 at 23:13

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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