我正在使用 Ruby 2.7.2、Rails 6.0.3、Debian 10 和 Nginx 和 Puma。
我正面临 Web 应用程序的问题。
环境变量由 Web 应用程序加载,rails console
而不是由 Web 应用程序加载。
我在.rbenv-vars
项目目录中创建了该文件,并检查了它是否已正确加载:
rbenv vars
export FOO=BAR
我在控制台上检查了一下,env 变量正在返回“BAR”。
rails c -e production
puts "FOO VARIABLE: #{ENV['FOO']}"
=> "BAR"
但是当我检查日志时,我看到ENV['FOO']
正在返回nil
.
class ApplicationController < ActionController::API
before_action :show_env_variables
def show_env_variables
puts "FOO VARIABLE: #{ENV['FOO']}"
end
end
你知道为什么 web 应用程序没有读取 env 变量吗?
这是我的puma.rb
#!/usr/bin/env puma
directory '/home/deploy/apps/app_name/current'
rackup "/home/deploy/apps/app_name/current/config.ru"
environment 'staging'
tag ''
pidfile "/home/deploy/apps/app_name/shared/tmp/pids/puma.pid"
state_path "/home/deploy/apps/app_name/shared/tmp/pids/puma.state"
stdout_redirect '/home/deploy/apps/app_name/shared/log/puma_access.log', '/home/deploy/apps/app_name/shared/log/puma_error.log', true
threads 0,16
bind 'unix:///home/deploy/apps/app_name/shared/tmp/sockets/puma.sock'
workers 0
restart_command 'bundle exec --keep-file-descriptors puma'
prune_bundler
on_restart do
puts 'Refreshing Gemfile'
ENV["BUNDLE_GEMFILE"] = ""
end
这是我的conf/etc/nginx/sites-enabled/domain.com
server {
ssl on;
ssl_certificate /etc/letsencrypt/live/domain.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/domain.com/privkey.pem;
listen 443;
listen [::]:443;
server_name domain.com;
root /home/deploy/apps/app_name/current/public;
try_files $uri/index.html $uri @puma_app_name;
client_max_body_size 4G;
keepalive_timeout 10;
error_page 500 502 504 /500.html;
error_page 503 @503;
location @puma_app_name {
proxy_http_version 1.1;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_set_header X-Forwarded-Proto http;
proxy_pass http://puma_app_name;
# limit_req zone=one;
access_log /home/deploy/apps/app_name/shared/log/nginx.access.log;
error_log /home/deploy/apps/app_name/shared/log/nginx.error.log;
}
location ^~ /assets/ {
gzip_static on;
expires max;
add_header Cache-Control public;
}
location = /50x.html {
root html;
}
location = /404.html {
root html;
}
location @503 {
error_page 405 = /system/maintenance.html;
if (-f $document_root/system/maintenance.html) {
rewrite ^(.*)$ /system/maintenance.html break;
}
rewrite ^(.*)$ /503.html break;
}
if ($request_method !~ ^(GET|HEAD|PUT|PATCH|POST|DELETE|OPTIONS)$ ){
return 405;
}
if (-f $document_root/system/maintenance.html) {
return 503;
}
}
server {
listen 0.0.0.0:80;
server_name domain.com;
rewrite ^ https://$host$request_uri? permanent;
}