【问题标题】:RoR - nginx issue when trying to deploy with unicornRoR - 尝试使用 unicorn 部署时出现 nginx 问题
【发布时间】:2013-04-06 13:25:54
【问题描述】:

我是 Ruby on Rails 的初学者,在部署我的 Rails 应用程序(使用 nginx + unicorn)时遇到一些困难。我不知道发生了什么,但这是我在启动 nginx 时在日志文件中遇到的错误类型:

2013/04/14 00:31:42 [error] 14469#0: *1 connect() to unix:/home/user/www/sahitoo/shared/sockets/unicorn.sock 
**failed (111: Connection refused)** while connecting to upstream, client: XX.XXX.XX.XX, server: myapp.com, 
request: "GET / HTTP/1.1", upstream: "http://unix:/home/user/www/sahitoo/shared/sockets/unicorn.sock:/", 
host: "www.XXXXX.com"

如果你能帮助找出问题,或者至少给我一些建议来跟踪它,那就太好了! 非常感谢。

我也发布了 nginx.conf 文件:

user www-data;
worker_processes 4;
pid /var/run/nginx.pid;

events {
  worker_connections 768;
  multi_accept on;
}

http {
  sendfile on;
  tcp_nopush on;
  tcp_nodelay on;
  keepalive_timeout 65;
  types_hash_max_size 2048;

  include /etc/nginx/mime.types;
  default_type application/octet-stream;

  access_log /var/log/nginx/access.log;
  error_log /var/log/nginx/error.log;

  gzip on;
  gzip_disable "msie6";

  gzip_http_version 1.1;
  gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

  ##
  # Virtual Host Configs
  ##

  include /etc/nginx/conf.d/*.conf;
  include /etc/nginx/sites-enabled/*;

  upstream sahitoo {
    server unix:/home/kar/www/sahitoo/shared/sockets/unicorn.sock;
  }
}

有 /etc/nginx/sites-enabled/sahitoo 文件:

server {
  listen 80;
  server_name myapp.com;

  access_log /var/log/nginx/sahitoo.access.log;
  error_log /var/log/nginx/sahitoo.error.log;

  root /www/sahitoo/public;

      # direct to maintenance if this file exists
      if (-f $document_root/system/maintenance.html) {
        rewrite  ^(.*)$  /system/maintenance.html last;
    break;
  }

  location / {
    proxy_redirect          http://sahitoo/               /;
    proxy_set_header        Host                                                            $host;
    proxy_set_header        X-Real-IP                                                     $remote_addr;
proxy_set_header  X-Forwarded-For                                               $proxy_add_x_forwarded_for;

    # If the file exists as a static file serve it directly
    if (-f $request_filename) {
      break;
    }

    if (!-f $request_filename) {
      proxy_pass http://sahitoo;
      break;
    }
  }

  error_page   500 502 503 504  /500.html;
  location = /500.html {
    root   /home/kar/www/sahitoo/public;
  }
}

【问题讨论】:

  • 好像独角兽没有运行。
  • 感谢 Intrepidd,独角兽也给了我错误,这可能确实是源问题... Je peux te contacter en privé?

标签: ruby-on-rails deployment nginx unicorn


【解决方案1】:

如果你在不同的用户上运行 ruby​​,可能会发生这种情况,可能是 root 并且它对当前用户没有任何特权,你确定你得到的结果来自

'ruby -v' 或'rails -v'

.

【讨论】:

    【解决方案2】:

    我建议看一下这个工作示例nginx.conf

    upstream unicorn-your_app {
      server unix:/tmp/unicorn.your_app.sock fail_timeout=0;
    }
    
    server {
      listen 80;
      server_name myapp.com;
      root /www/sahitoo/current/public;
    
      location ^~ /assets/ {
        gzip_static on;
        expires max;
        add_header Cache-Control public;
      }
    
      try_files $uri/index.html $uri @unicorn-yourapp;
      location @unicorn-yourapp {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://unicorn-your_app;
      }
    
      error_page 500 502 503 504 /500.html;
      client_max_body_size 4G;
      keepalive_timeout 10;
    }
    

    如您所见,有一些不同之处:

    • upstram 块;
    • 根指向your_dir/current/public
    • try_files 例程;
    • location @unicorn-yourapp;

    如果您想对该主题有更深入的了解,有很好的Railscast

    【讨论】:

    • 嗨,谢谢,所以我用这个例子替换了我当前的 nginx.conf ?
    • 我认为您应该查看命名约定,然后替换您的 /etc/nginx/sites-enabled/sahitoo 文件。我的示例是指导您完成配置过程,因此请再次检查输入错误的目录名称,然后替换它。
    猜你喜欢
    • 2015-03-29
    • 2019-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多