部署 部署 Nginx + Unicorn 时连接 sock 错误是什么原因呢?

newbie · 2014年08月24日 · 最后由 newbie 回复于 2014年08月25日 · 5406 次阅读

nginx 配置 (/etc/nginx/sites-enabled/default) 中:

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

server {


    listen 80;
    server_name localhost;

    # Application root, as defined previously
    root /root/my_app/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;

我访问网站时 nginx 的 error log /var/log/nginx/error.log 中出现下面错误:

14/08/24 07:55:50 [error] 26968#0: *1 connect() to unix:/tmp/unicorn.myapp.sock failed (111: Connection refused) while connecting to upstream, client: 123.456.789.012, server: localhost, request: "GET / HTTP/1.1", upstream: "http://unix:/tmp/unicorn.myapp.sock:/", host: "123.456.789.012"

查了下相关信息,这里有一样的情况:

http://stackoverflow.com/questions/5707938/unicorn-nginx-upstream-server-not-starting

但把 unix:/tmp/sockets/unicorn.sock 改成 127.0.0.1:3000,还是 Connect refused 错误。

具体原因在哪里呢?

没用过 unicorn 做配置,但明显,你的应用没有没有启动,反向代理自然不能转给应用了

set nginx user .

贴 unicorn.rb 内容

nginx 的配置要跟 unicorn.rb 的配置匹配才行

你用 unix socket 方式连接 unicorn 所以可以排查相对简单

  • 确认 Unicorn 启动成功,看 ps 和 unicorn 的 log 里是否正常
  • 确认/tmp/unicorn.myapp.sock 存在,不存在多半是权限问题

你帖子里没有描述过你有做过什么样的排查,这两点是最基本的排查,如果全部正常就需要更多信息来分析了

config/unicorn.rb 配置参考


worker_processes 1

APP_PATH = "/home/root/deploy/current"
working_directory APP_PATH

#/tmp/unicorn.myapp.sock;  这里sock名称和nginx的配置保持一致
listen "/tmp/unicorn.myapp.sock", :backlog => 64
listen 8000, :tcp_nopush => true
pid APP_PATH + "/tmp/pids/unicorn.pid"
# By default, the Unicorn logger will write to stderr.
# Additionally, ome applications/frameworks log to stderr or stdout,
# so prevent them from going to /dev/null when daemonized here:
stderr_path APP_PATH + "/log/unicorn.stderr.log"
stdout_path APP_PATH + "/log/unicorn.stderr.log"

惭愧,忘启动 unicorn 了。

bundle exec unicorn -E production -c config/unicorn.rb

现在出现了 nginx 403 forbidden 错误:

directory index of "/home/my_app/public" is forbidden, ...

/etc/nginx/nginx.conf 文件:

user nginx;
worker_processes 4;
pid /run/nginx.pid;

...

现在用部署用户 deploy 部署,rails 程序下面的文件及文件加都是 deploy 用户组的,要给 public 文件加特别的用户权限吗?

需要 登录 后方可回复, 如果你还没有账号请 注册新账号