-1

Any time when someone shares my website link onto facebook or twitter they get 502 Bad Gate way. I check the nginx error log to see whats going on and I got this.

error1615#0: *2375 connect() failed (111: Connection refused) while connecting to upstream, client: 69.171.230.119, server: example.com, request: "GET /328/example/tracks/2023 HTTP/1.1", upstream: "http://127.0.0.1:9000/snapshots?_escaped_fragment_=/328/example/tracks/2023", host: " example.com"

I try many methods on fixing this issue but I'm having no luck here.

#configs
server {
    listen                          80;
    server_name                     www.example.com;
    rewrite                         ^/(.*)      http://example.com/$1           permanent;
}

server {
    listen                          80;
    server_name                     www.api.example.com;
    rewrite                         ^/(.*)      http://api.example.com/$1       permanent;
}

server {
    set $maintenance 0;

    listen                          80;
    server_name                     example.com;

    access_log                      /srv/example-backend/logs/nginx.access.log;
    error_log                       /srv/example-backend/logs/nginx.error.log;

    root                            /srv/example-frontend/www/built/prod;
    index                           index.html;

    gzip                            on;
    gzip_types                      text/plain text/xml text/css application/json application/javascript application/x-javascript;

    location = /favicon.ico {
         log_not_found              off;
    }

    location /api {
        proxy_pass                  http://api.example.com/api/v1;
        proxy_redirect              off;
    }

    location ~* \.(eot|ttf|woff|js|css)$ {
    expires                     max;
        add_header                  Access-Control-Allow-Origin     *;
        add_header                  Access-Control-Allow-Headers    "X-Requested-With";
        add_header                  Access-Control-Allow-Methods    "GET, OPTIONS";
    }

    location / {
        set $prerender 0;

        if ($http_user_agent ~* "baiduspider|twitterbot|facebookexternalhit|rogerbot|linkedinbot|embedly|quora link preview|showyoubot|outbrain|pinterest") {
            set $prerender 1;
            set $prerender_uri $request_uri;
        }

        if ($args ~ "_escaped_fragment_=(.*)") {
            set $prerender 1;
            set $prerender_uri $1;
        }

        if ($uri ~ "\.(js|css|xml|less|png|jpg|jpeg|gif|pdf|doc|txt|ico|rss|zip|mp3|rar|exe|wmv|doc|avi|ppt|mpg|mpeg|tif|wav|mov|psd|ai|xls|mp4|m4a|swf|dat|dmg|iso|flv|m4v|torrent)") {
            set $prerender 0;
            set $maintenance 0;
        }

        if ($prerender = 1) {
            set $args _escaped_fragment_=$prerender_uri;
            rewrite                 ^       /snapshots;
        }

        expires                     -1;
        add_header                  Pragma                  "no-cache";
        add_header                  Cache-Control           "no-store, no-cache, must-revalidate, post-check=0, pre-check=0";

        if ($args ~* disable_maintenance) {
            set $maintenance 0;
        }

        if ($maintenance = 1) {
            rewrite            ^       /maintenance.html break;
        }

        try_files                   $uri                    /index.html =404;
    }

    location /snapshots {
        proxy_set_header            Host                    $http_host;

        proxy_pass                  http://127.0.0.1:9000;
        proxy_connect_timeout       30s;
        proxy_redirect              off;
    }
}

server {
    listen                          80;
    server_name                     api.example.com;

    gzip                            on;
    gzip_types                      text/plain text/xml text/css application/json application/javascript;
    client_max_body_size            2G;

    access_log                      /srv/example-backend/logs/nginx.api.access.log;
    error_log                       /srv/example-backend/logs/nginx.api.error.log;

    root                            /srv/example-backend/www/app/;
    index                           index.php;

    more_set_headers                'Access-Control-Allow-Origin: *';
    more_set_headers                'Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE';
    more_set_headers                'Access-Control-Allow-Headers: Authorization,Cache-Control,Origin,Content-Type,Accept';

    location = /favicon.ico {
        log_not_found               off;
        access_log                  off;
    }

    location = /robots.txt {
         alias /srv/example-frontend/www/built/prod/robots.txt;
    }

   location ~ /\. {
        deny                        all;
        access_log                  off;
        log_not_found               off;
   }

    location / {
        if ($request_method = OPTIONS) {
            more_set_headers       'Content-Length: 0';
            more_set_headers       'Access-Control-Max-Age: 1728000';
            more_set_headers       'Content-Type: text/plain';
            return                 204;
        }

        try_files                   $uri                    /index.php?$args;
    }

    location ~ /uploads/ {
        if ($request_method = OPTIONS) {
            more_set_headers       'Content-Length: 0';
            more_set_headers       'Access-Control-Max-Age: 1728000';
            more_set_headers       'Content-Type: text/plain';
            return                 204;
        }

        #count downloads
        if ($args ~* dl=1&entityTypeId=(\d+)&entityId=(\d+)&userId=(\d+)&filename=(.+)$) {
             set $entityTypeId      $1;
             set $entityId          $2;
             set $userId            $3;

             more_clear_headers     Content-Type;
             add_header             Content-Type            application/octet-stream;
             add_header             Content-Disposition     'attachment; filename=$4';

             post_action /count_downloads;
        }

    expires                     max;
        try_files                   $uri        =404;
    }

    location /count_downloads {
        proxy_pass                  http://api.example.com/api/v1/downloads/$entityTypeId/$entityId/$userId;
        internal;
    }

    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
        expires                     24h;
        log_not_found               off;
    }

    #yii specific
    location ~ /(protected|framework) {
        deny                        all;
        access_log                  off;
        log_not_found               off;
    }

    #m4a streaming
    location ~* \.m4a {
        types {
            audio/mp4               mp4     m4a;
        }

        mp4;
        mp4_buffer_size             1m;
        mp4_max_buffer_size         5m;
    }

    location ~ \.php$ {
        include                     fastcgi_params;
        try_files                   $uri                =404;
        fastcgi_split_path_info     ^(.+\.php)(/.+)$;
        fastcgi_param               PATH_INFO           $fastcgi_script_name;
        fastcgi_index               index.php;
        fastcgi_param               SCRIPT_FILENAME     $document_root$fastcgi_script_name;
        fastcgi_pass                unix:/var/run/php5-fpm.sock;
    }
}

1 Answer 1

0

This means nginx couldn't connect to 127.0.0.1 port 9000 to forward the request.

That's up to you to investigate on the reason why no service was bound to that socket by monitoring relevant metrics.

1
  • thanks for the help . One step closer finding the issues thanks again Mar 20, 2015 at 5:28

You must log in to answer this question.

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