Nginx, Unicorn and Rails = 502 Bad Gateway

11,575

Solution 1

I solved the problem.

It was because of my unicorn server started in development environment, not in production. Unicorn was trying to connect to development database, but credentials for the dev db in database.yml were missing. After I started unicorn in production env everything connected well.

Solution 2

It looks like socket problem, but it is usually (111: Connection refused) when socket is down, so I think this is app problem (high load, slow execution etc).

Try decrease backlog and see logs again for details:

listen "/var/sockets/unicorn.mypage.sock", backlog: 64

:backlog => number of clients

Share:
11,575
kashlo
Author by

kashlo

^ ^ dino

Updated on June 06, 2022

Comments

  • kashlo
    kashlo almost 2 years

    Im trying to setup Nginx, Unicorn and Rails application to work together. Nginx and Nnicorn are running, I checked that using ps command.

    But when trying to access my page Ive got 502 Bad Gateway

    Nginx error log has line:

    2015/03/18 19:53:26 [error] 14319#0: *1 connect() to unix:/var/sockets/unicorn.mypage.sock failed (11: Resource temporarily unavailable) while connecting to upstream

    What can be the problem?

    my /etc/nginx/conf.d/default.conf

    upstream app {
        server unix:/var/sockets/unicorn.mypage.sock fail_timeout=0;
    }
    
    server {
    
        listen 80;
        server_name mypage.com;
    
        # Application root, as defined previously
        root /home/rails/mypage/public;
    
        location ^~ /assets/ {
          gzip_static on;
          expires max;
          add_header Cache-Control public;
        }
    
        try_files $uri/index.html $uri @app;
    
        location @app {
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_redirect off;
            proxy_pass http://app;
        }
    
        error_page 500 502 503 504 /500.html;
        client_max_body_size 4G;
        keepalive_timeout 10;
    }
    

    /home/rails/mypage/config/unicorn.rb

    working_directory "/home/rails/mypage"
    
    pid "/home/rails/mypage/pids/unicorn.pid"
    
    stderr_path "/home/rails/mypage/log/unicorn.log"
    stdout_path "/home/rails/mypage/log/unicorn.log"
    
    listen "/var/sockets/unicorn.mypage.sock", backlog: 1024
    
    worker_processes 2
    
    timeout 30