问题原文
原帖链接 · pavel_karoukin · CC BY-SA 3.0
I run several docker containers with hostnames:
web1.local web2.local web3.local
Routing to these done based on hostname by nginx. I have a proxy in front of this setup (on different machine connected to internet) where I define upstream as:
upstream main {
server web1.local:80;
server web2.local:80;
server web3.local:80;
}
And actual virtual host description:
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://main;
}
}
Now, because containers receive hostname "main" instead of "web1.local", they do not respond properly to the request.
Question: how I can tell nginx to pass name of the upstream server instead of name of upstream group of servers in Host: header when proxying request?
社区高分解答
原帖链接 · Jens Bradler · CC BY-SA 3.0
Actually you can do that via proxy_set_header.
For more details look here: http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_set_header or see an example use-case here: https://stackoverflow.com/questions/12847771/configure-nginx-with-proxy-pass
I have included the dynamic approach into your above posted configuration:
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://main;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
Here is an example with a static host name:
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://main;
proxy_set_header Host www.example.com;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。
