当前位置: 代码迷 >> 综合 >> 艱難完成 nginx + puma 部署 rails 4的詳細記錄
  详细解决方案

艱難完成 nginx + puma 部署 rails 4的詳細記錄

热度:79   发布时间:2023-12-09 09:21:05.0

花了兩周時間 Google 部署方法,找的的許多方法都沒有用,最終被我用控制變數法,一條一條修改配置文件修改成功了。


首先是 /etc/nginx/vhosts/limlog.sloger.info.conf 和 config/puma.rb




#
# /etc/nginx/vhosts/limlog.sloger.info.conf
#upstream limlog {
     
server unix:///tmp/limlog.sock;
}server {
     
listen 80;
server_name limlog.sloger.info;root /srv/http/limlog.sloger.info/public;access_log /var/log/nginx/limlog-access.log;
error_log /var/log/nginx/limlog-error.log info;location / {
     
expires max;
add_header Cache-Control public;proxy_redirect off;
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_pass http://limlog;
}location ~ ^/assets/ {
     
expires 1y;
gzip_static on;
add_header ETag "";
add_header Cache-Control public;
break;
}
}




#!/usr/bin/env ruby -w#
# config/puma.rb
#rails_env = ENV['RAILS_ENV'] || 'development'threads 4, 4bind 'unix:///tmp/limlog.sock'
pidfile '/tmp/limlog.pid'
state_path '/tmp/limlog.state'activate_control_app


把 nginx 配置文件里的 root server_name upstream 修改成你的就行了,每個文件放在哪裡,文件頭部注釋裡面寫了。


然後是修改 config/environmens/production.rb


18 行 false 改為 true




# Disable Rails's static asset server (Apache or nginx will already do this).
config.serve_static_assets = true


29 行取消注釋




# Specifies the header that your server uses for sending files.
# config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache
config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx


然後是 app/controller/application_controller


第二行參數 with: :exception 去掉




protect_from_forgery


然手是 secret_key_base


我的做法是創建一個文件 env.sh




# 使用 rake secret 生成 key, 然後粘貼在 = 後面
export SECRET_KEY_BASE=# 下面可以 export 各種環境變數


啟動


啟動或者重啟 nginx


導入環境變數 source env.sh


啟動 rails bundle exec -C config/puma.rb -e production


現在就部署完畢了, 最令人頭疼的 assets 也解決了~

  相关解决方案