连接到上游时Nginx和Unicorn - (连接被拒绝)

时间:2013-10-16 04:48:40

标签: ruby-on-rails nginx unicorn

尝试在vps上部署我的第一个rails应用程序。我已按照以下设置中的说明进行操作。

https://www.digitalocean.com/community/articles/how-to-1-click-install-ruby-on-rails-on-ubuntu-12-10-with-digitalocean

但我的网站获得了504网关超时。

在nginx日志中,我得到以下内容:

2013/10/16 03:10:45 [error] 19627#0: *82 connect() failed (111: Connection refused) while connecting to upstream, client: 121.218.167.90, server: _, request: "GET / HTTP/1.1", upstream: "http://162.243.39.196:8080/", host: "162.243.39.196"

当我尝试运行独角兽时,我得到以下内容

E, [2013-10-16T04:26:28.530019 #30087] ERROR -- : adding listener failed addr=0.0.0.0:8080 (in use)

我的nginx默认文件包含以下内容

server {
        listen   80;
        root /home/rails/public;
        server_name _;
        index index.htm index.html;

        location / {
                try_files $uri/index.html $uri.html $uri @app;
        }

        location ~* ^.+\.(jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|mp3|flv|mpeg|avi)$ {
                        try_files $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_server;
    }

}

我的/home/unicorn/unicorn.conf有

listen "127.0.0.1:8080"
worker_processes 2
user "rails"
working_directory "/home/rails"
pid "/home/unicorn/pids/unicorn.pid"
stderr_path "/home/unicorn/log/unicorn.log"
stdout_path "/home/unicorn/log/unicorn.log"

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

您错过了proxy_pass http://app_server;中引用的上游区块。你可以把它放在像这样的服务器块之上。

upstream app_server {
    server 127.0.0.1:8080 fail_timeout=0;
}

server {
    listen   80;
    root /home/rails/public;
    server_name _;
    index index.htm index.html;
    ...