NGinx配置重定向域

时间:2013-09-10 10:10:09

标签: nginx reverse-proxy

我的网络上有2台服务器:

一台linux机器(192.168.0.2),其网站在端口8181上监听service1.domain.com 一台Windows机器(192.168.0.3),一个网站在端口8080上监听service2.domain.com

我想设置一个nginx反向代理,以便我可以像这样路由请求:

service1.domain.com --> 192.168.0.2:8181 with host header service1.domain.com

service2.domain.com --> 192.168.0.3:8080 with host header service2.domain.com

我尝试使用以下配置:

### General Server Settings             ###

worker_processes  1;

events {
  worker_connections  1024;
}

### Reverse Proxy Listener Definition  ###

http {
 server {
  listen        80;
  server_name       service1.domain.com;
  location / {
   proxy_pass       http://192.168.0.2:8181;
   proxy_set_header     host service1.domain.com;
  }
 }
server {
  listen        80;
  server_name       service2.domain.com;
  location / {
   proxy_pass       http://192.168.0.3:8080;
   proxy_set_header     host service2.domain.com;
  }
 }
}

但这似乎不起作用?

我在这里做错了有什么可怕的事吗?

1 个答案:

答案 0 :(得分:3)

这对我来说很好:

http {
    server {
        listen        80; 
        server_name       service1.domain.com;
        location / { 
            proxy_pass       http://192.168.0.2:8181;
            proxy_set_header   host  service1.domain.com
        }   
    }
    server {
        listen        80; 
        server_name       service2.domain.com;
        location / { 
            proxy_pass       http://192.168.0.3:8080;
            proxy_set_header     host service2.domain.com;
        }   
    }
}
试试看?