Error if secret_key_base is not configured in Rails production environment Unicorn

Asked 2 years ago, Updated 2 years ago, 122 views

Rails + Nginx + Unicorn is trying to build a production environment on Amazon Linux.

If you configure each of these settings and access the server from a browser, nothing will be displayed.
When I checked unicorn.stderr.log, I found that secret_key_base was not configured as shown below.

E, [2016-02-26T 11:48:42.067474#11474] ERROR --:app error:Missing `secret_token` and `secret_key_base` for 'production' environment, set these values in `config/secret.yml` (RuntimeError)
E, [2016-02-26T11:48:42.067574#11474] ERROR -- :/home/ec2-user/rails_test/vendor/bundle/ruby/2.2.0/gems/rails-4.2.3/lib/rails/application.rb:534:in`validate_secret_key_config!'
E, [2016-02-26T11:48:42.067602#11474] ERROR -- :/home/ec2-user/rails_test/vendor/bundle/ruby/2.2.0/gems/rails-4.2.3/lib/rails/application.rb:246:in`env_config'
The following...

However, SECRET_KEY_BASE is configured on the command line as follows:
SECRET_KEY_BASE=Long random string
export SECRET_KEY_BASE
You can also verify that the environment variable is set in printenv.
In addition, bundle exec rails -b 0.0.0.0-e production can access the URL where the error occurred without any problems.
The secret_key_base of the production remains the default as shown below.

production:
  secret_key_base:<%=ENV["SECRET_KEY_BASE"]%>

Unicorn and Nginx start as follows:
Unicorn starts with the command sudo bundle exec unicorn-c config/unicorn.rb-E production-D and
Nginx is started with sudo service nginx start.
Both have confirmed that the process has started.

Environment
Amazon Linux Rails 4.2.3
Unicorn 5.0.1
Nginx 1.9.11

Additional
I entered the secret_key_base for production directly into config/secret.yml and it was successfully accessed.It seems to be a problem on the Unicorn side.
Please let me know if you know how to avoid entering secret_key_base in config/secret.yml.

ruby-on-rails nginx unicorn

2022-09-30 14:25

1 Answers

It seems that uincorn is not able to recognize the environment variables properly, but when I fell in love with the same case in the past, it was as follows:

  • The Linux user defining the environment variable and the Linux user starting unicorn were different
  • Rails environment variables defined where they are not loaded at login

Because defining environment variables directly depends on the linux settings as described above, I use dotenv to define environment variables for rails in the .env file and load them every time Capistrano deploys them.For your information.


2022-09-30 14:25

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.