[RUBY] Rails5.1 + puma SSL connection in local production environment

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.

Connect with SSL

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.

Solved the problem that css / js disappeared

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)

Solved the problem of displaying files under public / images

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

Rails5.1 + puma SSL connection in local production environment
SSL in the local environment of Docker / Rails / puma
[Rails] Reset the database in the production environment
[Rails] Run LINEBot in local environment using ngrok
rails tutorial About account activation in production environment
500 Internal Server Error occurs in Rails production environment
Docker × Laravel HTTPS (SSL) communication in local environment
[Rails] What to do if you accidentally install bundle in the production environment in your local environment
Create a database in a production environment
[Rails 6] Customize Bootstrap in Rails + Bootstrap 5.0.0-alpha environment
[Environment construction] Uninstall rails from local
[Rails] How to execute "rails db: create" etc. in production environment EC2
Can't output PDF in production environment (EC2, Amazon Linux) with Rails
[Rails] How to reset the database in production environment (Capistrano version)
How to reflect seeds.rb in production environment
[Rails] About the error that the image is not displayed in the production environment
Puma --Nignx is an escape route when rails s -e production -d doesn't work in the environment
[Rails] About local: true described in form_with
Show Better Errors in Rails + Docker environment
Image is not displayed in production environment
Display the background image in the production environment
How to redirect to http-> https when SSL is enabled in Rails × Heroku environment
(Basic authentication) environment variables in rails and Docker
[Rails] How to use PostgreSQL in Vagrant environment
How to install Docker in the local environment of an existing Rails application [Rails 6 / MySQL 8]
[Rails] How to delete production environment MySQL data after putting it in the development environment
Group_by in Rails
Migration error after Activerecord association in Rails5 + Docker environment (2)
[Rails] Nginx, Puma environment deployment & server study [AWS EC2]
Sharing research on ER diagram automation in Rails environment
I get Mysql2 :: Error :: ConnectionError in the production environment
virtulbox + vagrant + Docker + nginx + puma + MySQL Rails environment construction
Migration error after Activerecord association in Rails5 + Docker environment
Regarding overcapacity when setting Rails tutorial environment in Cloud 9
[Error] The app is not displayed in the production environment
Build WordPress environment with Docker (Local) and AWS (Production)
Beginners use ubuntu in windows to prepare rails environment
I installed Squid on CentOS in my local environment
[Ruby on Rails] credential does not work in production environment even though I saved production.key