一个Nginx,2个应用程序/域

时间:2012-08-11 06:13:06

标签: nginx

我在同一台服务器上运行了2个Rails应用程序。我希望domain1.com的请求转发到我的Rails1应用程序,domain2.com请求我的Rails2应用程序。我像这样配置了我的nginx.conf:

upstream thin1 {
    server 0.0.0.0:3000;
    server 0.0.0.0:3001;
    server 0.0.0.0:3002;
}

upstream thin2 {
    server 0.0.0.0:3003;
    server 0.0.0.0:3004;
    server 0.0.0.0:3005;
}

server {
    listen          80;
    server_name     *.domain1.com;
    root            /path1/;        

    location / {
        try_files $uri/index.html $uri.html $uri @backend;
    }

    location @backend {
        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;
        proxy_pass http://thin1;
    }   
}

server {
    listen                  80;
    server_name             *.domain2.com;
    root                    /path2/;

    location / {
        try_files $uri/index.html $uri.html $uri @backend;
    }

    location @backend {
        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;
        proxy_pass http://thin2;
    }   
}

但所有请求(domain1.com和domain2.com)都转发到我的Rails1应用程序。有什么想法吗?

2 个答案:

答案 0 :(得分:0)

为每个主机名设置单独的服务器配置,并转发到每个主机的相应后端。

答案 1 :(得分:0)

为您的指定位置使用2个不同的名称应该修复它:

location @backend1 {
  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;
  proxy_pass http://thin1;
}

location @backend2 {
  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;
  proxy_pass http://thin2;
}

(不要忘记相应地更改每个服务器的try_files指令)