2014-09-12 89 views
1

如何将我的nginx.conf文件修改为适用于分段和生产环境的Capistrano文件,而无需在部署到其中一个时进行修改,而无需对其进行修改?配置nginx以用于Capistrano的Rails分段和生产部署

这是我当前文件:

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; 
    } 
    } 
} 
+0

你使用什么样的服务器'独角兽,puma'? – 2014-09-12 17:35:39

+0

此刻使用独角兽 – 8vius 2014-09-12 17:39:43

+0

我们有Nginx与Passenger合作 - 我会为您发布一个答案 – 2014-09-13 10:30:59

回答

-1

继教程在这个网站,以便清楚地阐明:GoRails VPS Installation,我们安装了乘客与Nginx的(!作品真的很好,太)

有了这个,我们可以使用下面的设置来接受传入的请求给我们的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; 
} 

,如果你想我可以提供更多的信息 - 这只是WH在我们现在正在使用

+0

与我目前的类似。我必须道歉,因为我的问题很模糊,我实际上需要一个能够在我的环境中通过capistrano进行部署的文件。这意味着我需要一个可以在'staging.mysite.com'和'sysite.com'上工作的配置,而无需在每次部署时都对其进行修改。 – 8vius 2014-09-14 21:04:07

1

而对于独角兽,对于任何工作环境的配置,只是改变域/根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; 
} 
+0

我目前有一个类似的配置。但我的问题实际上是怎样才能让一个配置适用于我的分段服务器,另一个配置适用于我的生产服务器,并且能够通过capistrano进行部署,而无需修改每个实例的文件。我想我应该在我的问题中澄清这一点。 – 8vius 2014-09-14 21:00:59

+0

那么,我通常对所有环境(不是操作系统,但LEMP软件,项目文件夹)都有类似的设置,并且不需要为Unicorn配置不同的配置,只需在/ etc/environments中设置所需的环境,例如: export RAILS_ENV ='生产' 和独角兽像任何其他Ruby相关的应用程序知道这个设置。至于nginx.conf,它只是不打扰Ruby环境。对不起,如果我的回答脱离主题:) – 2014-09-14 21:26:36

+0

不是真的这是我的坏,但我特别谈论设置服务器属性,它是不同的我的舞台和生产环境,每当我必须部署到一个或另一个我必须将其更改为使其正常工作。 – 8vius 2014-09-14 21:59:05

0

我使用nginx的和乘客,我觉得应该是similiar。下面是我所做的:

我创建了一个额外的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; 
} 

也许与其他的应用服务器,你可以使用,而不是“passenger_app_env”“RAILS_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 

我觉得你能适应这个符号链接任务,无论你设置你的服务器。您可能正在使用站点可用,并在那里启用站点的另一个符号链接。所以你需要根据你的具体设置来改变它。