Nginx 在配置代理时,以下 2 种方式完全一样
方式1:
proxy_pass http://internal.foo.com:80;
方式2:
proxy_set_header Host internal.foo.com;proxy_pass http://192.168.0.20:80;
本质上来讲,proxy_pass 相当于发起了一个 curl 请求,而 proxy_set_header 则是设置请求头的。
# 相当于方式1curl http://internal.foo.com:80# 等价于方式2curl http://192.168.0.20:80 \-H 'Host: internal.foo.com'
除了 Host Header 之外,其他来自于上游的 HTTP Header 都会被转发。下面就此结论进行相应的实验验证
架构图
192.168.0.10 (Nginx LB) ---> 192.168.0.20 (Nginx Backend)
Nginx LB
foo.com LB 站点配置
server {listen 80;server_name foo.com;location / {# 以下两种配置方法效果完全一致# proxy_pass http://internal.foo.com:80;proxy_set_header Host internal.foo.com;proxy_pass http://192.168.0.20:80;}}
/etc/hosts 添加以下解析
192.168.0.20 internal.foo.com
Nginx Backend
internal.foo.com 后端站点配置
server {listen 80;server_name internal.foo.com;index index.html index.htm;root /www/web/foo.com;location / {try_files $uri $uri/ =404;}}
为了观察转发后 Host 的变化,在日志中添加 http_host
log_format main '$remote_addr - $remote_user [$time_local] "$request" ''$status $body_bytes_sent "$http_referer" ''"$http_user_agent" "$http_x_forwarded_for" ''[$http_host] ';
/www/web/foo.com 的内容
root@f76414963a73:# cat /www/web/foo.com/index.htmlhello, foo.com
测试
测试主机 /etc/hosts 添加以下解析
192.168.0.10 foo.com
访问 foo.com 站点
suhua@g7-7588:~$ curl http://foo.comhello, foo.com
查看后端 internal.foo.com 访问日志
192.168.0.10 - - [07/Sep/2020:10:28:34 +0800] "GET / HTTP/1.0" 200 15 "-" "curl/7.58.0" "-" [internal.foo.com]
注意事项
以下配置方式会丢失 Host,后端收到的 Host 会是 192.168.0.10,即代理服务器的 IP。
proxy_pass http://192.168.0.20:80;
