摘抄地址,感谢作者!
一 被代理的服务器2的端口不是80
服务器1的配置:
[root@ans3 conf]# cat nginx.conf
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
sendfile on;
tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
# upstream backend {
# server 10.0.0.50:8080;
#}
server {
listen 80;
server_name a.test.com;
location / {
proxy_pass http://10.0.0.50:8080;
proxy_set_header X-Proxy-Host $proxy_host;
proxy_set_header Host $http_host;
index index.html index.htm;
}
}
}
服务器2的配置
[root@master conf]# cat nginx.conf
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 8080;
server_name www.test.com aa.test.com;
location / {
return 200 'http_host=[$http_host] host=[$host] proxy_host=[$http_x_proxy_host]\n';
}
}
}
不带请求头 Host
[root@ans3 conf]# curl -H 'Host:' --http1.0 http://a.test.com
http_host=[] host=[www.test.com] proxy_host=[10.0.0.50:8080]
携带请求头 Host
[root@ans3 conf]# curl -H 'Host:abc:123' --http1.0 http://a.test.com
http_host=[abc:123] host=[abc] proxy_host=[10.0.0.50:8080]
被代理的服务器2的端口是80
服务器1的配置
http {
include mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main;
sendfile on;
tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
# upstream backend {
# server 10.0.0.50:8080;
#}
server {
listen 80;
server_name a.test.com;
location / {
proxy_pass http://10.0.0.50:80;
proxy_set_header X-Proxy-Host $proxy_host;
proxy_set_header Host $http_host;
index index.html index.htm;
}
}
}
服务器2的配置
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
server {
listen 80;
server_name www.test.com aa.test.com;
location / {
return 200 'http_host=[$http_host] host=[$host] proxy_host=[$http_x_proxy_host]\n';
}
}
}
此时的 $proxy_host
会省略80端口
[root@ans3 conf]# curl -H 'Host:abc:123' --http1.0 http://a.test.com
http_host=[abc:123] host=[abc] proxy_host=[10.0.0.50]
ngx_http_core_module 文档中对 $host
的定义
in this order of precedence: host name from the request line, or host name from the “Host” request header field, or the server name matching a request
ngx_http_proxy_module 文档中对 $proxy_host
的定义
name and port of a proxied server as specified in the proxy_pass directive;
下面这个示例也能看出 $http_host
和 $host
的区别