Nginx + ユニコーン

Nginx + ユニコーン

私は nginx + passage から nginx + unicorn への移行に取り組んでいますが、少し行き詰まっています。

テスト サーバーを表示しようとすると、404 ページしか表示されません。vhost 構成に問題があることは確かですが、何が問題なのかわかりません。

これに関してご協力いただければ幸いです。

これは私のvhostファイルの現在のバージョンです

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 設定を変更して、別のポートまたはソケットをリッスンすることができます。

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

これはユニコーンの設定ファイルを使用します。http://unicorn.bogomips.org/Unicorn/Configurator.html設定ファイルの例。

ユニコーン設定ファイルを使用しない場合は、次の形式のコマンドラインスイッチが必要です。

-l, --listen ADDRESS

例えば:

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

pwd私が使用しているのは Rails ルート ディレクトリなので、相対パスを使用していることに注意してください。

関連情報