对于rails应用程序,nginx出现502错误

时间:2015-08-06 19:15:47

标签: ruby-on-rails nginx unicorn

我在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"

1 个答案:

答案 0 :(得分:0)

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

如果您的浏览器发送OPTIONS个请求并且您的服务器返回502个错误,则表示您的服务器是跨域的。然后,您的浏览器会先发送一个行前OPTIONS请求,以确定发送敏感数据是否安全。该请求应该通过Nginx,通过Unicorn并在它击中Rails之前点击Rack。 Rails会清理您的请求。如果您的来源被允许,Rack会将200返回到您的浏览器,之后浏览器可以发送实际的GETPOSTPATCHDELETE个请求你打算送。

要在CORS中打开Rack,您可以使用此初始化程序:

# 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