2012-09-06 116 views
1

我有一個Rails應用程序,它被部署在Capistrano上,與VPS的搭配非常類似,設置爲this Railscast。我有mydomain.co.uk和admin.mydomain.co.uk。子域名在本地使用lvh.me和標準Webbrick服務器正常工作,但在生產admin.mydomain.co.uk中顯示的內容與mydomain.co.uk完全相同。Rails 3與Nginx和Unicorn的子域名

我的routes.rb文件:

class AdminDomain 
    def self.matches?(request) 
    puts "Sub = #{request.subdomain}" 
    request.subdomain.present? && request.subdomain == "admin" 
    end 
end 

MyApp::Application.routes.draw do 

    constraints(AdminDomain) do 
    scope :module => "admin" do 
     match '', to: 'admin#index' 

     resources :users 
    end 
    end 

    # All the mydomain.co.uk routes... 

我的Nginx的配置:

upstream unicorn { 
    server unix:/tmp/unicorn.<%= application %>.sock fail_timeout=0; 
} 

server { 
    listen 80; 
    root <%= current_path %>/public; 

    server_name mydomain.co.uk admin.mydomain.co.uk; 

    listen 443 ssl; 
    ssl_certificate /home/deployer/mydomain_combined.crt; 
    ssl_certificate_key /home/deployer/mydomain.key; 
    proxy_set_header X-Forwarded-Proto $scheme; 

    auth_basic   "Restricted"; 
    auth_basic_user_file htpasswd; 

    location ^~ /assets/ { 
    gzip_static on; 
    expires max; 
    add_header Cache-Control public; 
    } 

    location = /favicon.ico { 
    expires max; 
    add_header Cache-Control public; 
    } 

    if (-f $document_root/system/maintenance.html) { 
    return 503; 
    } 

    error_page 503 @maintenance; 

    location @maintenance { 
    rewrite ^(.*)$ /system/maintenance.html last; 
    break; 
    } 

    try_files $uri/index.html $uri @unicorn; 
    location @unicorn { 
    proxy_set_header X-Forwarded-Proto $scheme; 
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
    proxy_set_header Host $http_host; 
    proxy_redirect off; 
    proxy_pass http://unicorn; 
    } 

    error_page 500 502 503 504 /500.html; 
    client_max_body_size 4G; 
    keepalive_timeout 10; 
} 

我唯一的想法是,Nginx的不流通的請求URL到麒麟。我有一個與SSL類似的問題,但通過添加proxy_set_header X-Forwarded-Proto $scheme;解決了這個問題。我如何讓子域在Nginx和Unicorn下的生產環境中正常運行?

+0

我也有這個問題,我發佈了。這裏有一個答案: http://stackoverflow.com/questions/18134046/multiple-rails-4-app-using-nginx-unicorn/18161635#18161635 – 2013-08-10 16:54:12

回答

2

看來,在我的設置生產request.subdomain被設置爲'admin.mydomain',而在開發中它只是'管理'。

因此添加此到routes.rb中使用正則表達式的作品在本地和我的生產服務器上:

constraints :subdomain => /admin.*/ do 
    scope :module => "admin" do 
    root to: 'admin#index' 
    end 
end 
+1

你需要改變':tld_length'參數作爲你的實時服務器比你的測試更深(.co.uk vs .me) – toxaq