【问题标题】:nginx and unicorn not workingnginx和独角兽不工作
【发布时间】:2015-12-01 08:59:00
【问题描述】:

我在 ubuntu14.04 上设置了 nginx 和 unicorn 来访问我的 rails 应用程序!

但是,我访问我的域,chrome 响应“连接被拒绝”

不知道为什么……

我该如何解决这个问题?

有我的 nginx.conf 和 unicorn 文件。

【nginx.conf】

events {
    worker_connections 768;
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

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

    ##
    # Logging Settings
    ##

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

    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_disable "msie6";

    # gzip_vary on;
    # gzip_proxied any;
    # gzip_comp_level 6;
    # gzip_buffers 16 8k;
    # 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;

    ##
    # nginx-naxsi config
    ##
    # Uncomment it if you installed nginx-naxsi
    ##

    #include /etc/nginx/naxsi_core.rules;

    ##
    # nginx-passenger config
    ##
    # Uncomment it if you installed nginx-passenger
    ##
    
    #passenger_root /usr;
    #passenger_ruby /usr/bin/ruby;

    ##
    # Virtual Host Configs
    ##

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

【myapp-unicorn】(/etc/nginx/site-enabled/myapp-unicorn)

upstream myapp.com {
    #my rails app
    server unix:/var/www/rails/myapp/tmp/sockets/unicorn.sock      fail_timeout=0;
}

server {
    listen 80;
    server_name myapp.com;
    location / {
        proxy_redirect off;
        proxy_set_header Host               $host;
        proxy_set_header X-Real-IP          $remote_addr;
        proxy_set_header X-Forwarded-Host   $host;
        proxy_set_header X-Forwarded-Server $host;
        proxy_set_header X-Forwarded-For    $proxy_add_x_forwarded_for;
        client_max_body_size 35M;
        proxy_pass http://myapp.com;
    }
}

【unicorn.rb】(/var/www/rails/myapp/config/unicorn.rb)

worker_processes 2

listen File.expand_path('tmp/sockets/unicorn.sock', ENV['RAILS_ROOT'])

stderr_path File.expand_path('log/unicorn.log', ENV['RAILS_ROOT'])
stdout_path File.expand_path('log/unicorn.log', ENV['RAILS_ROOT'])

preload_app true

before_fork do |server, worker|
    defined?(ActiveRecord::Base) and     ActiveRecord::Base.connection.disconnect!
    old_pid = "#{ server.config[:pid] }.oldbin"

    unless old_pid == server.pid
        begin
            Process.kill :QUIT, File.read(old_pid).to_i
        rescue Errno::ENOENT, Errno::ESRCH

        end
    end
end

after_fork do |server, worker|
    defined?(ActiveRecord::Base) and    ActiveRecord::Base.establish_connection
end

感谢您对我糟糕的英语的耐心等待。

添加

这是 unicorn.log(/var/www/rails/myapp/log/unicorn.log)

I, [2015-09-05T18:17:20.590239 #10832]  INFO -- : Refreshing Gem list
I, [2015-09-05T18:17:22.099133 #10832]  INFO -- : unlinking existing   socket=/var/www/rails/myapp/tmp/sockets/unicorn.sock
I, [2015-09-05T18:17:22.099389 #10832]  INFO -- : listening on addr=/var/www/rails/myapp/tmp/sockets/unicorn.sock fd=11
I, [2015-09-05T18:17:22.115503 #10832]  INFO -- : master process ready
I, [2015-09-05T18:17:22.118878 #10836]  INFO -- : worker=0 ready
I, [2015-09-05T18:17:22.127008 #10839]  INFO -- : worker=1 ready

这是 nginx.log(/var/log/nginx/error.log)

nginx 日志没有...

【问题讨论】:

  • 共享 Unicorn 和 Nginx 错误日志
  • 感谢您的回复!我添加了日志!
  • Nginx 错误日志不能为空,查看详情。看起来像:connect() failed (111: Connection refused) while connection to upstream, client .... 需要 Nginx 日志中的更多详细信息。尝试重命名上游,不要让它看起来像 FQDN。

标签: nginx unicorn


【解决方案1】:

如果你的错误是

connect() failed (111: Connection refused) while connecting to upstream

尝试运行独角兽监听

listen /tmp/sockets/unicorn.sock

而不是

listen File.expand_path('tmp/sockets/unicorn.sock', ENV['RAILS_ROOT'])

因为有时 nginx 由于权限而无法读取该套接字文件。如果您在 tmp 文件夹中有套接字文件,那是相当安全的。还将您 NGINX 指向

server unix:/tmp/sockets/unicorn.sock      fail_timeout=0;  

而不是

server unix:/var/www/rails/myapp/tmp/sockets/unicorn.sock      fail_timeout=0;  

如果仍有错误请回复
快乐部署;)

【讨论】:

  • 这是错误答案! Unicorn master 本身已经启动成功。
  • @Anatoly 虽然 unicorn 和 nginx 启动成功,但连接被拒绝是因为 nginx 无法找到 sock 文件或无法读取该 sock 文件.....
  • @Anatoly 我已经写了**如果这是错误**在关注你评论stackoverflow.com/questions/32410967/…
猜你喜欢
  • 2014-08-19
  • 2013-01-10
  • 1970-01-01
  • 1970-01-01
  • 2011-01-27
  • 2015-09-27
  • 2023-03-09
  • 2014-01-15
  • 2012-01-05
相关资源
最近更新 更多