0

我的 Rails 应用程序根据子域切换功能,例如 blah1.mysite.com 和 blah2.mysite.com

我想使用 request.host 访问我的 rails 控制器中的子域,但对于我在浏览器中尝试的每个子域,主机始终是 mysite_qa。我该如何解决?

我认为这是我的 nginx 配置的一个问题,我是一个 nginx 菜鸟,所以任何帮助将不胜感激。

这是 nginx 中可用的站点记录:

upstream mysite_qa {
        ip_hash;                  # If you enable the second one and don't make sessions common
        server localhost:9002;
}

server {
        client_max_body_size 20M;
        listen 80;
        #listen 443 ssl;          # If you end up enabling SSL
        server_name qa.mysite.com;
        server_name blah1.mysite.com;
        server_name blah2.mysite.com;
        location / {
                proxy_pass http://mysite_qa;
                allow all;
        }
        location /nginx_status {
                stub_status on;
                access_log   off;
                allow 10.8.0.0/24;
                allow 172.16.0.0/16;
                deny all;
        }

}

这是我的独角兽文件:

worker_processes 4

APP_PATH = "/var/www/qa.mysite.com"
working_directory APP_PATH

stderr_path APP_PATH + "/log/unicorn.stderr.log"
stdout_path APP_PATH + "/log/unicorn.stderr.log"

pid APP_PATH + "/tmp/pid/unicorn.pid"

listen 9002, :tcp_nopush => true

# nuke workers after 30 seconds instead of 60 seconds (the default)
timeout 30

preload_app true
GC.respond_to?(:copy_on_write_friendly=) and
  GC.copy_on_write_friendly = true

before_fork do |server, worker|
  # the following is highly recomended for Rails + "preload_app true"
  # as there's no need for the master process to hold a connection
  defined?(ActiveRecord::Base) and
    ActiveRecord::Base.connection.disconnect!
end

after_fork do |server, worker|
  defined?(ActiveRecord::Base) and
    ActiveRecord::Base.establish_connection
end

我正在运行 Ubuntu 12.04.1

nginx版本:nginx/1.1.19

4

1 回答 1

2

nginx 可以设置一个包含原始主机名的标头,然后您的应用程序可以读取该标头:

http://www.simplicidade.org/notes/archives/2011/02/nginx_proxy_host_header.html

    location / {
            proxy_pass http://mysite_qa;
            proxy_set_header Host $http_host;
            allow all;
    }
于 2013-05-07T14:45:01.863 回答