質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Webサーバー

Webサーバーとは、HTTPリクエストに応じて、クライアントに情報を提供するシステムです。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Amazon EC2

Amazon EC2は“Amazon Elastic Compute Cloud”の略称です。Amazon Web Services(AWS)の一部であり、仮想化されたWebサーバーのコンピュータリソースをレンタルできるサービスです。

CSS

CSSはXMLやHTMLで表現した色・レイアウト・フォントなどの要素を指示する仕様の1つです。

Q&A

0回答

597閲覧

EC2を2つ用いてrailsアプリ運用がしたい。 2つのサーバーを用いたとき,CSS・JSがほとんど反映されなくなる

raiki.imamichi

総合スコア0

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Webサーバー

Webサーバーとは、HTTPリクエストに応じて、クライアントに情報を提供するシステムです。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Amazon EC2

Amazon EC2は“Amazon Elastic Compute Cloud”の略称です。Amazon Web Services(AWS)の一部であり、仮想化されたWebサーバーのコンピュータリソースをレンタルできるサービスです。

CSS

CSSはXMLやHTMLで表現した色・レイアウト・フォントなどの要素を指示する仕様の1つです。

0グッド

0クリップ

投稿2021/07/20 15:19

編集2021/07/20 15:45

前提・実現したいこと

EC2を2つ用いてrailsアプリ運用がしたい。
2つのサーバーを用いたときでも、CSS・JSを読みこめるようにしたい。

現状
AWSで下記の構成でrailsアプリのデプロイまで完了しました。
イメージ説明

発生している問題・エラーメッセージ

両方のサーバーで rails server -e production をして、スマホで確認すると、 CSSやJSが表示されないことが多々ある。 また一度表示されなくなるとsafariなどのアプリをタスクキルしない限り、 そのページは再読み込みしたとしても、CSS・JSが読み込まれない。 タスクキルしても表示されないこともある。 気持ち画像が多いページ程、CSS・JSが反映されなくなっている。 片方のみのEC2で rails server -e production をした時は問題なくCSS・JSは表示される。

該当のソースコード

ruby

1ruby 2log/production.log 3[af1fe402-43d0-4797-900d-096e58574a44] ActionController::RoutingError (No route matches [GET] "/packs/media/slick/ajax-loader-c5cd7f53.gif"): 4[af1fe402-43d0-4797-900d-096e58574a44] 5F, [2021-07-20T13:53:30.078738 #14195] FATAL -- : [0eec3015-2980-4d50-88ea-dbdd38eee56e] 6[0eec3015-2980-4d50-88ea-dbdd38eee56e] ActionController::RoutingError (No route matches [GET] "/packs/media/fonts/slick-d41f55a7.ttf"): 7[0eec3015-2980-4d50-88ea-dbdd38eee56e] 8F, [2021-07-20T13:53:36.578798 #14195] FATAL -- : [9170e9f1-6e53-4513-a842-5eb6b2a60fd6] 9[9170e9f1-6e53-4513-a842-5eb6b2a60fd6] ActionController::RoutingError (No route matches [GET] "/packs/media/slick/ajax-loader-c5cd7f53.gif"): 10[9170e9f1-6e53-4513-a842-5eb6b2a60fd6] 11F, [2021-07-20T13:53:36.716955 #14195] FATAL -- : [45afdf57-8b00-43cf-93e7-b113b7a56154] 12[45afdf57-8b00-43cf-93e7-b113b7a56154] ActionController::RoutingError (No route matches [GET] "/packs/media/fonts/slick-d41f55a7.ttf"): 13[45afdf57-8b00-43cf-93e7-b113b7a56154] 14F, [2021-07-20T13:54:02.049332 #14195] FATAL -- : [ce1c4b7b-19e1-46e3-b1a6-f73dac82ec46] 15[ce1c4b7b-19e1-46e3-b1a6-f73dac82ec46] ActionController::RoutingError (No route matches [GET] "/packs/js/application-b610d75862aa1fd2e383.js.map"): 16[ce1c4b7b-19e1-46e3-b1a6-f73dac82ec46] 17F, [2021-07-20T13:54:02.207922 #14195] FATAL -- : [713e50a8-f7b5-4cf9-b21f-dafc41e19fb2] 18[713e50a8-f7b5-4cf9-b21f-dafc41e19fb2] ActionController::RoutingError (No route matches [GET] "/packs/media/slick/ajax-loader-c5cd7f53.gif"): 19[713e50a8-f7b5-4cf9-b21f-dafc41e19fb2] 20F, [2021-07-20T13:54:02.267868 #14195] FATAL -- : [ff052d0f-281e-489c-bd80-5d30bb6e8115] 21[ff052d0f-281e-489c-bd80-5d30bb6e8115] ActionController::RoutingError (No route matches [GET] "/packs/media/fonts/slick-b7c9e1e4.woff"): 22[ff052d0f-281e-489c-bd80-5d30bb6e8115] 23F, [2021-07-20T13:54:02.338087 #14195] FATAL -- : [303e39fa-3183-46f7-80fa-2e371385ad18] 24[303e39fa-3183-46f7-80fa-2e371385ad18] ActionController::RoutingError (No route matches [GET] "/packs/media/fonts/slick-d41f55a7.ttf"): 25[303e39fa-3183-46f7-80fa-2e371385ad18] 26F, [2021-07-20T13:54:06.528087 #14195] FATAL -- : [1c41adf1-a49d-46e9-b945-91f5bbc3a596] 27[1c41adf1-a49d-46e9-b945-91f5bbc3a596] ActionController::RoutingError (No route matches [GET] "/packs/js/application-b610d75862aa1fd2e383.js.map"): 28[1c41adf1-a49d-46e9-b945-91f5bbc3a596] 29F, [2021-07-20T13:54:06.725809 #14195] FATAL -- : [c52df05e-c1f8-4e3e-af2b-f19bd1197ab6] 30[c52df05e-c1f8-4e3e-af2b-f19bd1197ab6] ActionController::RoutingError (No route matches [GET] "/packs/media/slick/ajax-loader-c5cd7f53.gif"): 31[c52df05e-c1f8-4e3e-af2b-f19bd1197ab6] 32F, [2021-07-20T13:54:06.778260 #14195] FATAL -- : [c5580b05-6b49-4be0-8e3f-4c2614a11b98] 33[c5580b05-6b49-4be0-8e3f-4c2614a11b98] ActionController::RoutingError (No route matches [GET] "/packs/media/fonts/slick-b7c9e1e4.woff"): 34[c5580b05-6b49-4be0-8e3f-4c2614a11b98] 35F, [2021-07-20T13:54:06.934064 #14195] FATAL -- : [e67eff19-ae0e-4355-803a-c5577b6ba99a] 36[e67eff19-ae0e-4355-803a-c5577b6ba99a] ActionController::RoutingError (No route matches [GET] "/packs/media/fonts/slick-d41f55a7.ttf"): 37[e67eff19-ae0e-4355-803a-c5577b6ba99a] 38F, [2021-07-20T14:12:02.274062 #14195] FATAL -- : [5aa25c62-b307-4822-b719-9651c7270166] 39[5aa25c62-b307-4822-b719-9651c7270166] ActionController::RoutingError (No route matches [GET] "/favicon.ico"): 40[5aa25c62-b307-4822-b719-9651c7270166] 41F, [2021-07-20T14:34:35.516776 #14195] FATAL -- : [78bae128-d8c7-4fc8-a4c0-455c85b64198] 42[78bae128-d8c7-4fc8-a4c0-455c85b64198] ActionController::RoutingError (No route matches [GET] "/.env"): 43[78bae128-d8c7-4fc8-a4c0-455c85b64198] 44F, [2021-07-20T14:34:35.912826 #14195] FATAL -- : [a920c07c-d9f5-4e0a-9510-343fe65ba791] 45[a920c07c-d9f5-4e0a-9510-343fe65ba791] ActionController::RoutingError (No route matches [POST] "/"): 46 47config/environments/production.rb 48Rails.application.configure do 49 # Settings specified here will take precedence over those in config/application.rb. 50 51 # Code is not reloaded between requests. 52 config.cache_classes = true 53 54 config.eager_load = true 55 56 57 config.consider_all_requests_local = false 58 config.action_controller.perform_caching = true 59 60 61 config.require_master_key = true 62 63 config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present? 64 65 66 # Do not fallback to assets pipeline if a precompiled asset is missed. 67 config.assets.compile = true 68 config.assets.initialize_on_precompile=false 69 # Enable serving of images, stylesheets, and JavaScripts from an asset server. 70 # config.action_controller.asset_host = 'http://assets.example.com' 71 72 # Specifies the header that your server uses for sending files. 73 # config.action_dispatch.x_sendfile_header = 'X-Sendfile' # for Apache 74 # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for NGINX 75 76 # Store uploaded files on the local file system (see config/storage.yml for options). 77 config.active_storage.service = :local 78 79 # Mount Action Cable outside main process or domain. 80 # config.action_cable.mount_path = nil 81 # config.action_cable.url = 'wss://example.com/cable' 82 # config.action_cable.allowed_request_origins = [ 'http://example.com', /http://example.*/ ] 83 84 # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies. 85 # config.force_ssl = true 86 87 # Use the lowest log level to ensure availability of diagnostic information 88 # when problems arise. 89 config.log_level = :error 90 91 # Prepend all log lines with the following tags. 92 config.log_tags = [ :request_id ] 93 94 # Use a different cache store in production. 95 # config.cache_store = :mem_cache_store 96 97 # Use a real queuing backend for Active Job (and separate queues per environment). 98 # config.active_job.queue_adapter = :resque 99 # config.active_job.queue_name_prefix = "アプリ名_production" 100 101 config.action_mailer.perform_caching = false 102 103 # Ignore bad email addresses and do not raise email delivery errors. 104 # Set this to true and configure the email server for immediate delivery to raise delivery errors. 105 # config.action_mailer.raise_delivery_errors = false 106 107 # Enable locale fallbacks for I18n (makes lookups for any locale fall back to 108 # the I18n.default_locale when a translation cannot be found). 109 config.i18n.fallbacks = true 110 111 # Send deprecation notices to registered listeners. 112 config.active_support.deprecation = :notify 113 114 # Use default logging formatter so that PID and timestamp are not suppressed. 115 config.log_formatter = ::Logger::Formatter.new 116 117 # Use a different logger for distributed setups. 118 # require 'syslog/logger' 119 # config.logger = ActiveSupport::TaggedLogging.new(Syslog::Logger.new 'app-name') 120 121 if ENV["RAILS_LOG_TO_STDOUT"].present? 122 logger = ActiveSupport::Logger.new(STDOUT) 123 logger.formatter = config.log_formatter 124 config.logger = Logger.new("log/production.log", 5, 10 * 1024 * 1024) 125 end 126 127 # Do not dump schema after migrations. 128 config.active_record.dump_schema_after_migration = false 129 130 131 132end 133 134config/puma.rb 135 136max_threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 } 137min_threads_count = ENV.fetch("RAILS_MIN_THREADS") { max_threads_count } 138threads min_threads_count, max_threads_count 139 140#port ENV.fetch("PORT") { 3000 } 141bind "unix://#{Rails.root}/tmp/sockets/puma.sock" 142# Specifies the `environment` that Puma will run in. 143# 144environment ENV.fetch("RAILS_ENV") { "development" } 145 146# Specifies the `pidfile` that Puma will use. 147pidfile ENV.fetch("PIDFILE") { "tmp/pids/server.pid" } 148 149 150# Allow puma to be restarted by `rails restart` command. 151plugin :tmp_restart

nginx

1 2/etc/nginx/conf.d/rails.conf 3upstream puma { 4 # pumaの設定で指定したsocketファイルを指定 5 6 server unix:///var//www/アプリ名/tmp/sockets/puma.sock; 7 8} 9 10server { 11 # nginxが待ち受けしたいポートを指定 12 13 listen 3000 default_server; 14 15 listen [::]:3000 default_server; 16 17 server_name puma; 18 19 location ~* .(html|scss|js)$ { 20 root /var/www/アプリ名/public; 21 expires 10d; 22 access_log off; 23 break; 24 } 25 26 location / { 27 proxy_read_timeout 300; 28 29 proxy_connect_timeout 300; 30 31 proxy_redirect off; 32 33 proxy_set_header Host $host; 34 35 proxy_set_header X-Forwarded-Proto $http_x_forwarded_proto; 36 37 proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 38 39 # 上記server_name で設定した名前で指定 40 41 proxy_pass http://puma; 42 } 43}

試したこと

assetsファイルに問題があると考え、
2つのサーバーのyarn、nodeのバージョンが一致している事を確認した。
相違していたためバージョンを揃えて、再度
rails assets:precompile RAILS_ENV=production
をしたが直らなかった。

次にキャッシュが影響しているのではないかと考え、
config/environments/production.rb

config.cache_classes = true
config.eager_load = true
config.action_controller.perform_caching = true
をfalseにしてみたが直らなかった。

補足情報(FW/ツールのバージョンなど)

nginx
Rails 6.1.3.1
ruby 3.0.0p0
Bundler version 2.2.16
yarn 1.22.10
node v12.21.0

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Noin

2021/07/20 18:55

セッション周りやLBの設定がわかりません。 気になったのは、セッション周りですかね。 動的処理の下に置いてる場合は、外に置いた方が良いです。(nginx側) セッション管理がローカルの場合はサーバの外、memcachedやredisに置くかLBをNLBにして一度アクセスしたサーバは維持するようにするなどすると良いです。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問