我有一个rails应用程序,它通过Capistrano部署到VPS,其设置与this Railscast非常相似。我有mydomain.co.uk和admin.mydomain.co.uk。子域名使用lvh.me和标准Webbrick服务器在本地工作正常,但在生产中,admin.mydomain.co.uk显示与mydomain.co.uk完全相同的内容。
我的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没有将请求网址传递给独角兽。我有一个与SSL类似的问题,但通过添加proxy_set_header X-Forwarded-Proto $scheme;
解决了这个问题。如何在Nginx和Unicorn下的生产环境中使子域正常运行?
答案 0 :(得分:2)
似乎在我的生产设置中request.subdomain
设置为'admin.mydomain',而在开发中它只是'admin'。
因此,使用正则表达式将其添加到routes.rb中可以在本地和我的生产服务器上运行:
constraints :subdomain => /admin.*/ do
scope :module => "admin" do
root to: 'admin#index'
end
end