使用Rails 3,Unicorn& Nginx,如何访问子域的主机

时间:2013-05-07 14:35:25

标签: ruby-on-rails-3 nginx unicorn

我的rails应用根据子域切换功能,例如blah1.mysite.com& blah2.mysite.com

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

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

以下是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

1 个答案:

答案 0 :(得分: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;
    }