【问题标题】:Configure nginx for Rails staging and production deployment with Capistrano使用 Capistrano 为 Rails 登台和生产部署配置 nginx
【发布时间】:2015-03-06 00:02:15
【问题描述】:

如何修改我的nginx.conf 文件以在我的暂存环境和生产环境中使用Capistrano 进行部署,而无需在部署到其中一个或另一个时对其进行修改?

这是我当前的文件:

upstream app_server {
  server unix:/tmp/unicorn.mysite.socket fail_timeout=0;
}

server {
  listen 80;
  server_name mysite.com;

  root /home/deploy/apps/mysite/current/public;

  location / {
    proxy_set_header  X-Real-IP  $remote_addr;
    proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    proxy_redirect off;

    if (-f $request_filename/index.html) {
      rewrite (.*) $1/index.html break;
    }

    if (-f $request_filename.html) {
      rewrite (.*) $1.html break;
    }

    if (!-f $request_filename) {
      proxy_pass http://app_server;
      break;
    }
  }
}

【问题讨论】:

  • 你使用什么样的服务器unicorn, puma
  • 目前使用独角兽
  • 我们已经让 Nginx 与Passenger 合作 - 我会为您发布答案
  • @8vius 你最终解决了这个问题吗?我有同样的问题。

标签: ruby-on-rails nginx capistrano


【解决方案1】:

我创建了capistrano-unicorn-nginx plugin,以防你不想手动编辑 nginx 配置文件。

【讨论】:

    【解决方案2】:

    对于 Unicorn,适用于任何环境的工作配置,只需将 domain/root 更改为 example.com:

    server {
        server_name example.com;
        root /var/www/example.com/current/public;
    
        location ~* ^/assets/ {
          expires 1y;
          add_header Cache-Control public;
          add_header Last-Modified "";
          add_header ETag "";
          break;
        }
    
        try_files $uri/index.html $uri.html $uri @app;
    
        location @app {
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header Host $http_host;
          proxy_redirect off;
          proxy_pass http://app_server;
        }
    
        # Rails error pages
        error_page 500 502 503 504 /500.html;
        location = /500.html {
          root /var/www/example.com/current/public;
        }
    }
    
    upstream app_server {
       server unix:/tmp/unicorn.example.sock fail_timeout=0;
    }
    

    【讨论】:

    • 我目前有类似的配置。但我的问题实际上是如何拥有一个适用于我的登台服务器和另一个适用于我的生产服务器的配置,并且能够通过 capistrano 进行部署,而无需修改每个实例的文件。我想我应该在我的问题中澄清这一点。
    • 好吧,我通常对所有环境(不是操作系统,而是 LEMP 软件,项目文件夹)都有类似的设置,并且不需要为 Unicorn 设置不同的配置,只需在 /etc/environments 中设置所需的环境,例如: export RAILS_ENV='production' 和任何其他 Ruby 相关应用程序一样,Unicorn 都知道这个设置。至于 nginx.conf,它只是不关心 Ruby 环境。对不起,如果我的回答离题:)
    • 这不是我的错,但我专门讨论的是设置服务器属性,它对于我的登台和生产环境是不同的,每当我必须部署到其中一个或另一个时,我都必须更改它让它正常工作。
    【解决方案3】:

    我用的是nginx和passenger,我觉得应该差不多。这是我所做的:

    我为 staging 创建了一个额外的 nginx 配置文件,名为 nginx-staging.conf:

    server {
      listen 80 default_server;
      # server_name www.mydomain.com;
      passenger_enabled on;
      passenger_app_env staging;
      root /var/www/<app-name>/current/public;
    
      error_page 500 502 503 504 /500.html;
      # client_max_body_size 4G;
      keepalive_timeout 10;
    }
    

    也许对于另一个应用服务器,你会使用“rails_env”而不是“passenger_app_env”,我不确定。

    然后我有一个单独的任务来根据环境对文件进行符号链接:

    namespace :setup do
    
        ...
    
        desc "Symlinks config files for Nginx"
        task :symlink_config do
            on roles :app do
    
                # Here we fetch the rails_env and see if it is staging,
                # if it is, you use that nginx-staging.conf, else you use the standard one for production
                if fetch(:rails_env) == :staging
                    execute "ln -nfs #{current_path}/config/nginx-staging.conf /etc/nginx/sites-enabled/#{fetch :application}"
                else
                    execute "ln -nfs #{current_path}/config/nginx.conf /etc/nginx/sites-enabled/#{fetch :application}"
                end
            end
        end
    end
    
    namespace :deploy do
    
       ...
    
        before :deploy, "deploy:check_revision"
        after :deploy, "deploy:restart"
        after :rollback, "deploy:restart"
    
        # Add this task to run before restarting nginx
        before :restart, "setup:symlink_config"
    end
    

    我认为您可以调整此符号链接任务以适应您设置服务器的方式。您可能正在使用 sites-available 并在那里有另一个符号链接到启用的站点。所以你需要根据你的具体设置来改变它。

    【讨论】:

      【解决方案4】:

      按照这个站点上很好的教程:GoRails VPS Installation,我们用 Nginx 安装了Passenger(也很好用!)

      有了这个,我们能够使用以下设置来接受传入到我们的 Rails 应用程序的请求:

      #etc/nginx/sites-available/your_site
      server {
              listen 80;
              server_name yoursite.com;
              root /apps/your_app/public;
              passenger_enabled on;   
              include fastcgi_params;
      }
      

      如果您愿意,我可以提供更多信息 - 这正是我们现在正在使用的内容

      【讨论】:

      • 与我目前拥有的类似。我必须道歉,因为我的问题含糊不清,我实际上需要一个文件,它可以在通过 capistrano 部署时适用于我的两种环境。这意味着我需要一个可以在 'staging.mysite.com' 和 'mysite.com' 上运行的配置,而无需在每次部署时都进行修改。
      猜你喜欢
      • 1970-01-01
      • 2016-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-09-08
      相关资源
      最近更新 更多