使用带有多个端口的nginx上游组

我想用多个端口使用相同的上游定义:

upstream production { server 10.240.0.26; server 10.240.0.27; } server { listen 80; server_name some.host; location / { proxy_pass http://production:1234; } } server { listen 80; server_name other.host; location / { proxy_pass http://production:4321; } } 

使用该configurationnginx -t抛出: upstream "production" may not have port 1234和退出代码1。

当我尝试将proxy_pass URL(或其部分)定义为一个variables时:

 set $upstream_url "http://production:1234" set $upstream_host production; set $upstream_port 1234; proxy_pass $upstream_url; # or proxy_pass http://production:$upstream_port; # or proxy_pass http://$upstream:$upstream_port; 

Nginx尝试通过parsing器parsing我的上游名称:

  *16 no resolver defined to resolve production, client: ...., server: some.host, request: "GET / HTTP/1.1", host: "some.host" 

对我来说, proxy_pass文件听起来好像完全不应该发生;

服务器名称,端口和传递的URI也可以使用variables指定:

proxy_pass http:// $ host $ uri; 甚至像这样:

proxy_pass $ request; 在这种情况下,在所描述的服务器组中search服务器名称,并且如果未find,则使用parsing器来确定。

经过nginx版本testing:

  • 1.6
  • 1.9
  • 1.10
  • 1.11

我错过了什么吗? 有没有办法解决这个问题?

您必须在upstream每个server条目中定义端口。 如果你不用nginx将它设置为80 。 所以server 10.240.0.26; 实际上是指server 10.240.0.26:80;

你可以定义几个上游块:

 upstream production_1234 { server 10.240.0.26:1234; server 10.240.0.27:1234; } upstream production_4321 { server 10.240.0.26:4321; server 10.240.0.27:4321; } server { listen 80; server_name some.host; location / { proxy_pass http://production_1234; } } server { listen 80; server_name other.host; location / { proxy_pass http://production_4321; } } 

另一个select是configuration你的本地DNS来parsing主机名production到几个IP,在这种情况下,nginx将使用它们全部。

http://nginx.org/r/proxy_pass :如果一个域名parsing为多个地址,所有这些地址都将以循环方式使用。

 server { listen 80; server_name some.host; location / { proxy_pass http://production:1234; } } 

当您使用上游时,端口在上游块中定义:

 upstream production { server 10.240.0.26:8080; server 10.240.0.27:8081; } 

换句话说,nginx将proxy_pass参数parsing为上游组或host:port对。 当你使用一个variables作为参数时,它只能parsing为host:port对。