使用Capistrano为Rails登台和生产部署配置nginx

时间:2014-09-12 17:22:22

标签: ruby-on-rails nginx capistrano

我如何修改我的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;
    }
  }
}

4 个答案:

答案 0 :(得分:1)

如果您不想手动编辑nginx配置文件,我发了capistrano-unicorn-nginx plugin

答案 1 :(得分:1)

对于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;
}

答案 2 :(得分:0)

我使用nginx和乘客,我认为应该是类似的。这是我做的:

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

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

我认为您可以调整此符号链接任务,但是您可以设置服务器。您可能正在使用site-avaliable并在那里启用了另一个符号链接。所以你需要根据你的具体设置进行更改。

答案 3 :(得分:-1)

按照本网站上详细介绍的教程: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;
}

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