在Linux上使用nginx重新创build类似于提琴手的主机和端口

我正在尝试在Linux上使用nginx作为反向代理来设置主机和端口重新映射 。

到目前为止,我已经有了一个有效的贫民窟解决scheme,使用if指令 ,这是邪恶的 。

有没有更好的解决scheme,而不使用?

我曾尝试过–Nginxconfiguration

/etc/nginx/nginx.conf (或者一些/etc/nginx/conf.d/*.conf文件):

 server { listen 3000; server_name dev.example.com test.example.com prod.example.com location / { if ($http_host ~ dev.example.com) { proxy_pass 127.0.0.1:13000; } if ($http_host ~ test.example.com) { proxy_pass 127.0.0.1:23000; } if ($http_host ~ prod.example.com) { proxy_pass 127.0.0.1:33000; } } } 

/ etc / hosts

 127.0.0.1 dev.example.com 127.0.0.1 test.example.com 127.0.0.1 prod.example.com 

我想做什么 – 提琴手HOSTSconfiguration

对于熟悉Fiddler的人,我试图模仿这个HOSTSconfiguration :

 localhost:13000 dev.example.com:3000 localhost:23000 test.example.com:3000 localhost:33000 prod.example.com:3000 

使用单独的server块:

 server { server_name dev.example.com; listen 3000; proxy_pass http://127.0.0.1:13000; } server { server_name test.example.com; listen 3000; proxy_pass http://127.0.0.1:23000; } 

另一个是prod.example.com

如果这些站点configuration包含公共元素,请将其包含在另一个文件中,并使用include指令将这些元素应用于每个虚拟服务器。

利用地图模块:

http上下文:

 map $http_host $proxy_target { "dev.example.com" "127.0.0.1:13000"; "test.example.com" "127.0.0.1:23000"; "prod.example.com" "127.0.0.1:33000"; } 

server环境:

 proxy_pass $proxy_target; 

此外,你可以尝试只区分端口,并使用类似的东西

 proxy_pass 127.0.0.1:$proxy_port; 

但我不确定这样的join是否有效。