2014-09-12 87 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 

我覺得你能適應這個符號鏈接任務,無論你設置你的服務器。您可能正在使用站點可用,並在那裏啓用站點的另一個符號鏈接。所以你需要根據你的具體設置來改變它。