When I upgraded from Rails 5.0 to 5.1 and tried to check the operation of production locally, I stumbled upon something, so I will summarize the solutions to various problems.
First, when I hit rails s -e production
, I got an error that the WEB page could not be displayed due to a security problem with this connection. It seems to be useless unless it is set to https.
For the basic method, I referred to this article. Enable SSL / HTTPS in Rails5 + puma's local environment
In the above article, I issued the certificate with openssl, but I got an error, so I issued the certificate with mkcert. This article was helpful for how to issue a certificate with mkcert. Good news for those who are warning about SSL in the local environment with my certificate
First, issue a certificate with mkcert according to the above article, and place the server.key and server.crt files in an appropriate location under the app folder.
Set puma.rb by referring to Enable SSL / HTTPS in Rails5 + puma local environment.
Start the server with bundle exec pumactl start -e production
.
I was able to connect with SSL, but I got the error HTTP parse error, malformed request ()
and application.js and application.css were not loaded.
Since the server of js and css was http://0.0.0.0:3000, modify the setting of asset_host as follows. I defined it in an environment variable so that it can be changed locally and in the production environment.
config/environments/production.rb
config.action_controller.asset_host = "https://#{ENV['HOST_URI']}"
reference -[StackOverflow :: Ruby on Rails production environment cannot refer to public / assets](https://ja.stackoverflow.com/questions/44638/ruby-on-rails%E3%81%AE%E6%9C% AC% E7% 95% AA% E7% 92% B0% E5% A2% 83% E3% 81% A7public-assets% E3% 81% 8C% E5% 8F% 82% E7% 85% A7% E3% 81% A7% E3% 81% 8D% E3% 81% AA% E3% 81% 84)
With the above support, css and js came out, but the image files placed under public / images were not displayed yet. Starting with Rails 5.1, static files that are not precompiled with asset_pipeline are written as follows when called by the helper.
= image_tag 'hogehoge.png', skip_pipeline: true
However, even if this was set, it didn't work, so it seems to be another problem.
** Correspondence 1 **
Set config.public_file_server.enabled
to true
config/environments/production.rb
- config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present?
+ config.public_file_server.enabled = true
** Correspondence 2 ** redo assets: precompile.
#If you want to take the plunge and erase everything once, click here first
RAILS_ENV=production bundle exec rake assets:clobber
# assets:clean erases older versions of files. If you do clobber, you don't need clean
RAILS_ENV=production bundle exec rake assets:precompile assets:clean
With the above support, all assets are now displayed.
reference
-Rails4 asset pipeline related settings summary (Heroku compatible)
Recommended Posts