繁体   English   中英

nginx和unicorn bad gatewayconnect()到unix:/tmp/mobile.socket失败并且失败(111:拒绝连接)

[英]nginx and unicorn bad gatewayconnect() to unix:/tmp/mobile.socket failed AND failed (111: Connection refused)

我(尝试)在rackspace上设置一个ubuntu 11.04服务器来运行带有nginx和unicorn的rails 3.2应用程序。 我找到了这个很棒的博客http://techbot.me/2010/08/deployment-recipes-deploying-monitoring-and-securing-your-rails-application-to-a-clean-ubuntu-10-04-install-using -nginx-and-unicorn /它帮助我大规模地解决了mysql设置问题,我认为除了网关错误之外我已经完成了所有工作

nginx错误日志显示

2012/02/25 14:38:34 [crit] 29139#0: *1 connect() to unix:/tmp/mobile.socket failed (2: No such file or directory) while connecting to upstream, client: xx.xx.xxx.xx, server: localhost, request: "GET / HTTP/1.1", upstream: "http://unix:/tmp/mobile.socket:/", host: xx.xx.xxx.xx

(我已经x了域名)

我想这可能是一个用户权限的事情,但文件实际上并不存在,我不知道应该如何创建它。 我不愿意手动创建它,因为我觉得这样做会修复症状而不是修复原因

还应该注意,我在服务器上创建的用户有sudo权限,需要使用sudo启动nginx,不确定这是否正确? 关于我应该寻找什么来解决这个问题的任何指示都非常感谢。 为了完整性,我的配置文件看起来像/etc/init.dunicorn

#! /bin/sh
### BEGIN INIT INFO
# Provides: unicorn
# Required-Start: $local_fs $remote_fs $network $syslog
# Required-Stop: $local_fs $remote_fs $network $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: starts the unicorn web server
# Description: starts unicorn
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/bin/unicorn_rails
DAEMON_OPTS="-c /home/testapp/mobile/current/unicorn.rb -E production -D"
NAME=unicorn_rails
DESC=unicorn_rails
PID=/home/testapp/mobile/shared/pids/unicorn.pid

case "$1" in
  start)
echo -n "Starting $DESC: "
$DAEMON $DAEMON_OPTS
echo "$NAME."
;;
  stop)
echo -n "Stopping $DESC: "
        kill -QUIT `cat $PID`
echo "$NAME."
;;
  restart)
echo -n "Restarting $DESC: "
        kill -QUIT `cat $PID`
sleep 1
$DAEMON $DAEMON_OPTS
echo "$NAME."
;;

和/ etc / nginx / sites-available / default中的nginx配置

# as we are going to use Unicorn as the application server
# we are not going to use common sockets
# but Unix sockets for faster communication
upstream mobile {
  # fail_timeout=0 means we always retry an upstream even if it failed
  # to return a good HTTP response (in case the Unicorn master nukes a
  # single worker for timing out).

  # for UNIX domain socket setups:
  server unix:/tmp/mobile.socket fail_timeout=0;
}

server {
    # if you're running multiple servers, instead of "default" you should
    # put your main domain name here
    listen 80 default;
# you could put a list of other domain names this application answers
server_name localhost;

root /home/testapp/mobile/current/public;
access_log /var/log/nginx/mobile_access.log;
rewrite_log on;

location / {
    #all requests are sent to the UNIX socket
    proxy_pass http://mobile;
    proxy_redirect off;

    proxy_set_header Host $host:$proxy_port;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

    client_max_body_size 10m;
    client_body_buffer_size 128k;

    proxy_connect_timeout 90;
    proxy_send_timeout 90;
    proxy_read_timeout 90;

    proxy_buffer_size 4k;
    proxy_buffers 4 32k;
    proxy_busy_buffers_size 64k;
    proxy_temp_file_write_size 64k;
}

# if the request is for a static resource, nginx should serve it directly
# and add a far future expires header to it, making the browser
# cache the resource and navigate faster over the website
location ~ ^/(images|javascripts|stylesheets|system)/ {
  root /home/testapp/mobile/current/public;
  expires max;
  break;
}

}

更新我的unicorn.rb文件

# See http://unicorn.bogomips.org/Unicorn/Configurator.html for complete
# documentation.
worker_processes 4
# Help ensure your application will always spawn in the symlinked
# "current" directory that Capistrano sets up.
working_directory "/home/testapp/mobile/current"
# listen on both a Unix domain socket and a TCP port,
# we use a shorter backlog for quicker failover when busy
listen "/tmp/mobile.socket", :backlog => 64
# nuke workers after 30 seconds instead of 60 seconds (the default)
timeout 30
# feel free to point this anywhere accessible on the filesystem
user 'testapp', 'testapp'
shared_path = '/home/testapp/mobile/shared'
pid "#{shared_path}/pids/unicorn.pid"
stderr_path "#{shared_path}/log/unicorn.stderr.log"
stdout_path "#{shared_path}/log/unicorn.stdout.log"

根据建议我手动创建了mobile.socket文件,现在我收到以下错误

[error] 1083#0: *4 connect() to unix:/tmp/mobile.socket failed (111: Connection refused) while connecting to upstream

这只是mobile.socket文件中的权限吗? 如果是这样,我需要什么权限?

更新2 nginx和独角兽似乎都运行正常

testapp @ airmob:〜/ mobile / current $ ps aux | grep nginx

root      6761  0.0  0.1  71152  1224 ?        Ss   18:36   0:00 nginx: master process /usr/sbin/nginx
testapp   6762  0.0  0.1  71492  1604 ?        S    18:36   0:00 nginx: worker process
testapp   6763  0.0  0.1  71492  1604 ?        S    18:36   0:00 nginx: worker process
testapp   6764  0.0  0.1  71492  1604 ?        S    18:36   0:00 nginx: worker process
testapp   6765  0.0  0.1  71492  1604 ?        S    18:36   0:00 nginx: worker process
testapp  13071  0.0  0.0   8036   600 pts/0    R+   21:21   0:00 grep --color=auto nginx

我在相关的配置文件(unicorn.rb和nginx默认)中将mobile.socket重命名为mobile.sock,一切都很好,不需要创建任何套接字文件,它只是按预期工作。

如果应用服务器没有运行(在我的情况下是独角兽),也会发生这种情况。 Unicorn创建套接字,nginx查找它。 如果套接字不在那里,nginx会大惊小怪,所以如果您正在阅读这个寻找解决方案,请确保您的应用服务器(独角兽)正在运行,并确保所有套接字名称在各种配置文件中匹配(unicorn.rb和无论nginx.conf文件中是否有提到的套接字)

您指定它应该使用位于/tmp/mobile.socket的套接字所以是的,解决方案是简单地创建它。

upstream mobile {
  # for UNIX domain socket setups:
  server unix:/tmp/mobile.socket fail_timeout=0;
}

我假设您在unicorn.rb引用了相同的套接字。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM