问题原文
原帖链接 · jayme · CC BY-SA 3.0
I would like to use the same upstream definition with multiple ports:
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;
}
}
Using that configuration nginx -t throws: upstream "production" may not have port 1234 and returns with exitcode 1.
When I try to define the proxy_pass URL (or parts of) as a variable like:
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 tries to resolve the name of my upstream via resolver:
*16 no resolver defined to resolve production, client: ...., server: some.host, request: "GET / HTTP/1.1", host: "some.host"
For me, the proxy_pass doc sounds as if exactly that should not happen;
A server name, its port and the passed URI can also be specified using variables:
proxy_pass http://$host$uri; or even like this:
proxy_pass $request; In this case, the server name is searched among the described server groups, and, if not found, is determined using a resolver.
Tested with nginx versions:
- 1.6
- 1.9
- 1.10
- 1.11
Am I missing something? Is there a way to work around this?
已采纳解答
原帖链接 · Alexey Ten · CC BY-SA 3.0
You must define port in every server entry in upstream. If you don't nginx will set it to 80. So server 10.240.0.26; actually means server 10.240.0.26:80;.
You could define several upstream blocks though:
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;
}
}
Another option is to configure your local DNS to resolve hostname production to several IPs, and in this case nginx will use them all.
http://nginx.org/r/proxy_pass: If a domain name resolves to several addresses, all of them will be used in a round-robin fashion.
server {
listen 80;
server_name some.host;
location / {
proxy_pass http://production:1234;
}
}
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
