【问题标题】:502 error with nginx for a rails applicationRails 应用程序的 nginx 出现 502 错误
【发布时间】:2015-08-06 19:15:47
【问题描述】:

我正在 centos 上使用 rails、unicorn 和 nginx 构建应用程序。我对做服务器端的事情很陌生,但我正在尝试follow this tutorial 并让应用程序运行。

这是我的 nginx default.conf 文件:

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

server {


    listen 8080;
    server_name localhost;

    root /www/rqm3/;

    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;
}  

这是我的独角兽文件:

# Set the working application directory
# working_directory "/path/to/your/app"
working_directory "/www/rqm3"

# Unicorn PID file location
# pid "/path/to/pids/unicorn.pid"
pid "/www/rqm3/pids/unicorn.pid"

# Path to logs
# stderr_path "/path/to/log/unicorn.log"
# stdout_path "/path/to/log/unicorn.log"
stderr_path "/www/rqm3/log/unicorn.log"
stdout_path "/www/rqm3/log/unicorn.log"

# Unicorn socket
listen "/tmp/unicorn.[app name].sock"
listen "/tmp/unicorn.rqm3.sock"

# Number of processes
# worker_processes 4
worker_processes 2

# Time-out
timeout 30

这是我的 nginx 错误日志

2015/08/06 14:37:44 [crit] 24375#0: *30 connect() to unix:/tmp/unicorn.rqm3.sock failed (13: Permission denied) while connecting to upstream, client: 192.168.2.213, server: localhost, request: "GET / HTTP/1.1", upstream: "http://unix:/tmp/unicorn.rqm3.sock:/", host: "192.168.1.29:8080"

【问题讨论】:

  • 愚蠢的问题,但是您是否正在启动您的 Rails(机架)应用程序?另外,你的独角兽文件中的listen "/tmp/unicorn.[app name].sock" 行是错字吗?
  • 应用启动了,有问题的那行是直接从教程里复制过来的,我试试不看那行,看看有没有什么变化。
  • 刚刚添加了错误日志,似乎我没有正确的权限,但我将其设置为“775”

标签: ruby-on-rails nginx unicorn


【解决方案1】:

在我的 Rails 5 + Puma + Nginx 设置中,我只在从 www.example.comapi.example.com 执行 cross-origin requests 时看到 502

如果您的浏览器正在发送OPTIONS 请求并且您的服务器返回502 错误,这表明您的服务器是跨域的。然后,您的浏览器将首先发送飞行前OPTIONS 请求,以查看发送敏感数据是否安全。该请求应该通过 Nginx,通过 Unicorn 并在到达 Rails 之前到达 Rack。 Rails 会清理您的请求。如果您的来源是允许的,Rack 将返回200 到您的浏览器,之后浏览器可以发送实际的GETPOSTPATCHDELETE 您打算发送的请求。

要在Rack 中打开你的CORS,你可以使用这个初始化器:

# config/initializers/cors.rb
# https://github.com/cyu/rack-cors

Rails.application.config.middleware.insert_before 0, Rack::Cors do
  allow do
    origins 'example.com'

    resource '*',
      headers: :any,
      methods: [:get, :post, :put, :patch, :delete, :options, :head]
  end
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-14
    • 2013-07-16
    • 2016-02-16
    • 2012-07-10
    • 2018-01-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多