Nginx 502 Bad Gateway simple Rails App in CentOS 7.x

February 4, 2015 8.4k views

In CentOS 7.x I've installed a Rails app which I can access through [ip]:8080. It works.
But when I set the domain name as a server_name (instead of localhost) I get a 502 Bad Gateway problem.

I already restarted nginx. And the application is running with unicorn_rails. My rails log shows nothing.
PD: This app is from this tutorial.

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

upstream app {
    # Path to Unicorn SOCK file, as defined previously
    server unix:/tmp/unicorn.myapp.sock fail_timeout=0;
}

server {

    listen 80;
    server_name mydomain.com www.mydomain.com;

    # Application root, as defined previously
    root /var/www/my_app;

    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;
}
2 comments
  • Are you seeing anything in your Nginx error logs? /var/log/nginx/error.log Generally, a 502 error means that there is a problem with the connection between the app and the proxy server. Is the app using /tmp/unicorn.myapp.sock to communicate, does it actually exist?

    by Mitchell Anicas
    This guide focuses on identifying and troubleshooting the most commonly encountered HTTP error codes, i.e. 4xx and 5xx status codes, from a system administrator's perspective. There are many situations that could cause a web server to respond to a request with a particular error code--we will cover common potential causes and solutions.
  • Hi @asb.

    No, unicorn.myapp.sock seems to be missing.

    [root@mydroplet tmp]# pwd
    /var/www/my_app/tmp
    [root@mydroplet tmp]# ls
    cache  pids  sessions  sockets
    

    This is what my /var/log/nginx/error.log shows:
    (sorry for all this pasted code)

    2015/02/04 13:24:58 [error] 640#0: *1 open() "/usr/share/nginx/html/tasks" failed (2: No such file or directory),  , server: localhost, request: "GET /tasks HTTP/1.1", host: "my-ip"
    2015/02/04 13:24:59 [error] 640#0: *1 open() "/usr/share/nginx/html/tasks" failed (2: No such file or directory),  , server: localhost, request: "GET /tasks HTTP/1.1", host: "my-ip"
    2015/02/04 13:30:14 [warn] 1715#0: conflicting server name "localhost" on 0.0.0.0:80, ignored
    2015/02/04 13:30:14 [warn] 1716#0: conflicting server name "localhost" on 0.0.0.0:80, ignored
    2015/02/04 13:30:59 [crit] 1747#0: *1 stat() "/root/my_app/public//index.html" failed (13: Permission denied),  , server: mydomain.com, request: "GET / HTTP/1.1", host: "mydomain.com"
    2015/02/04 13:30:59 [crit] 1747#0: *1 stat() "/root/my_app/public/" failed (13: Permission denied),  , server: mydomain.com, request: "GET / HTTP/1.1", host: "mydomain.com"
    2015/02/04 13:30:59 [crit] 1747#0: *1 connect() to unix:/tmp/unicorn.myapp.sock failed (2: No such file or directory) while connecting to upstream,  , server: mydomain.com, request: "GET / HTTP/1.1", upstream: "http://unix:/tmp/unicorn.myapp.sock:/", host: "mydomain.com"
    
    

    What confuses me is that when I use the ip, the Rails application runs, but it doesn't when domain name is set.

1 Answer

Use

setenforce permissive

and run!

Have another answer? Share your knowledge.