LoginSignup
29
24

More than 1 year has passed since last update.

Rails 4.2以降: Rails+Unicorn で、publicディレクトリの中身が404になる場合の解決策

Last updated at Posted at 2015-09-29

状況

  • public 以下すべてが 404 状態で、cssもjavascriptもfaviconも無効の場合。
  • public 以下に直接ファイルを置いても、ブラウザからアクセスできない場合。

設定変更

Railsの設定ファイルを変更する。(Rails 4.2.3)

config/environments/production.rb
- config.serve_static_files = ENV['RAILS_SERVE_STATIC_FILES'].present?
+ config.serve_static_files = true

(もともとの設定では、環境変数によって動作が変わるようになっている)

その後、unicorn を再起動しよう。


Rails4.1 以前では設定値が違うようだ。(Rails 4.1.0)

config/environments/production.rb
- config.serve_static_assets = false
+ config.serve_static_assets = true

次の記事を参考にした。
Rails(Apache+Unicorn)で、public以下のディレクトリの中身が404になる場合の解決策


unicorn の設定を変えずに、ウェブサーバーに任せることも出来る。
nginx の場合はこんな感じで location ~ ^/assets/ を書く。

/etc/nginx/conf.d/example.conf
server {
  listen          80;
  server_name     xxx.xxx.xxx.xxx;
  server_name     example.com;
  root /path/to/public;

  location ~ ^/assets/ {
    root   /path/to/public;
  }
}

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

メンター受付

29
24
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
29
24