Nginx + 獨角獸

Nginx + 獨角獸

我正在努力從 nginx + guest 遷移到 nginx + unicorn,但我已經到了一個有點卡住的地步。

當我嘗試查看測試伺服器時,除了 404 頁面之外什麼都沒有得到。我確信我的虛擬主機配置中有問題,但我只是不明白問題是什麼。

非常感謝任何對此的幫助。

這是我的虛擬主機檔案的當前版本

upstream unicorn-staging {
  server unix:/data/appname/staging/current/tmp/sockets/unicorn-staging.sock fail_timeout=0;
}

server {
  listen 80 deferred;
  listen 443;
  ssl on;
  root /data/appname/staging/current/public;
  server_name foo;
  access_log /data/appname/staging/current/log/unicorn-staging-access.log;
  error_log /data/appname/staging/current/log/unicorn-staging-error.log;
  client_max_body_size 4G;
  ssl_certificate /data/appname/staging/shared/certs/appname.crt;
  ssl_certificate_key /data/appname/staging/shared/certs/appname.key;

  location / {
    proxy_pass http://unicorn-staging;
    proxy_redirect     off;
    proxy_set_header   Host             $host;
    proxy_set_header   X-Real-IP        $remote_addr;
    proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
    proxy_set_header X-Forwarded-Proto https; # for SSL, add this

    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;

    index  index.html index.htm;
  }

  location ~ \.(jpg|png|mp3|ogg)$ {
    valid_referers server_names;
    if ($invalid_referer) {
      return 403;
    }
  }

  location ~ \.(jpg|png|mp3|ogg|js|css|html|gif)$ {
    gzip_static on;
    expires max;
    add_header  Cache-Control public;
  }

  location ~ ^/(images|javascripts|stylesheets|assets)/  {
    root /data/appname/staging/current/public; # for asset pipeline and other static files
    expires max;
    break;
  }
  # redirect server error pages to the stat
  error_page  500 502 503 504  /50x.html;
}

答案1

您的獨角獸配置很可能未正確設定。

當 nginx 運行時,這種情況發生在我的伺服器上,但 unicorn 沒有運行。原因是 nginx 嘗試將請求傳遞到套接字,但 unicorn 不在那裡接收它。

預設情況下,unicorn 僅偵聽連接埠 8080。您可以變更 unicorn 設定以偵聽不同的連接埠或套接字。

如果您使用的是獨角獸設定檔 ( config/unicorn.rb),那麼在該檔案中,您應該有類似的內容(注意您必須更改套接字路徑):

listen File.expand_path("tmp/sockets/unicorn.sock", RAILS_ROOT)

出於調試目的,我也讓它監聽端口。

listen File.expand_path("tmp/sockets/unicorn.sock", RAILS_ROOT)
listen 3000, :tcp_nopush => true

要在生產 Web 伺服器上啟動 unicorn,需要執行類似以下內容的命令:

bundle exec unicorn -E production -c config/unicorn.rb

這使用 unicorn 配置檔。看http://unicorn.bogomips.org/Unicorn/Configurator.html取得範例設定檔。

如果您不使用 unicorn 配置文件,則需要以下格式的命令列開關:

-l, --listen ADDRESS

例如:

bundle exec unicorn -l tmp/sockets/unicorn.sock

請注意,我使用的是相對路徑,因為pwd我使用的是rails根目錄。

相關內容