我正在尝试替换 nginx 配置中标头中的主机变量,使其变为some.random.sub.example.net
。net-some.random.sub.example.com
我已尝试以下操作:
if ($host ~* "^([^.]+(\.[^.]+)*)\.example.net$"){
set $sub $1;
set $host1 "http://net-$sub.example.com";
break;
}
然后在位置指令中,我有:
location / {
proxy_set_header HOST $host1;
some conf
}
但它不起作用!
编辑
完整配置:
server {#http
listen 80 default_server;
server_name *.example.com;
large_client_header_buffers 4 16k;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
if ($host ~* "^([^.]+(\.[^.]+)*)\.example.net$"){
set $sub $1;
set $host1 http://cn-$sub.example.com;
break;
}
location / {
proxy_pass http://ghs.google.com;
proxy_set_header HOST $host1;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Server $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Real-HOST $host;
proxy_set_header Proxy-Hostname $scheme://$http_host;
proxy_set_header X-Forwarded-Proto https;
proxy_pass_request_headers on;
proxy_redirect off;
proxy_intercept_errors on;
proxy_redirect false;
}
}
答案1
请参阅 http proto -- 在Host
指令中您没有传递 http/https -- 仅传递服务器名称
答案2
好的,经过多次实验,我解决了。
set $host1 http://cn-$sub.example.com;
应该 :
set $host1 cn-$sub.example.com;
有什么解释吗?