更改传递给后端的主机
Changing the Host passed to the backend
我有我的前端服务器 运行 nginx。后端在同一个 VPN 上的另一台机器上。这是它的配置:
server {
listen 80;
server_name *.vpn.domain.com;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://10.8.25.102:8100/;
proxy_redirect http://10.8.25.102:8100/ http://$server_name/;
}
}
我想将不同的主机传递给后端...我希望后端接收对 subdomain.vpn.domain.com
主机的请求 subdomain.local.domain.com
有什么办法吗?我正在寻找正则表达式替换(甚至是子字符串替换),但出乎意料的是收效甚微……我认为这将是小菜一碟。我认为解决方案是
server {
listen 80;
server_name *.vpn.domain.com;
set $my_host $http_host;
replace $my_host .vpn. .local.
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $my_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://10.8.25.102:8100/;
proxy_redirect http://10.8.25.102:8100/ http://$server_name/;
}
}
只是我还没有找到 replace $my_host .vpn. .local.
的正确语法我真的不关心多个替换...我不会有 a.vpn.a.vpn.domain.com
终于想通了,我可以
if ($http_host ~ ^(.*)\.vpn\.(.*)$) {
set $my_host .local.;
}
然后,由于有 CSRF 验证,我还需要重写 Referer
...所以这就是它最终的样子
server {
listen 80;
server_name *.vpn.domain.com;
set $my_host $http_host;
if ($http_host ~ ^(.*)\.vpn\.(.*)$) {
set $my_host .local.;
}
set $referer $http_referer;
set $referer_host no;
if ($http_referer ~ ^(https?://)([^/]+)(/.*)$) {
set $referer_host ;
set $rewritten_referer $my_host;
}
if ($referer_host = $http_host) {
set $referer $rewritten_referer;
}
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $my_host;
proxy_set_header X-NginX-Proxy true;
proxy_set_header Referer $referer;
proxy_set_header IS_SECURE no;
proxy_pass http://10.8.25.102:8100/;
proxy_redirect https://$my_host/ https://$http_host/;
proxy_redirect http://$my_host/ http://$http_host/;
}
}
我有我的前端服务器 运行 nginx。后端在同一个 VPN 上的另一台机器上。这是它的配置:
server {
listen 80;
server_name *.vpn.domain.com;
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://10.8.25.102:8100/;
proxy_redirect http://10.8.25.102:8100/ http://$server_name/;
}
}
我想将不同的主机传递给后端...我希望后端接收对 subdomain.vpn.domain.com
主机的请求 subdomain.local.domain.com
有什么办法吗?我正在寻找正则表达式替换(甚至是子字符串替换),但出乎意料的是收效甚微……我认为这将是小菜一碟。我认为解决方案是
server {
listen 80;
server_name *.vpn.domain.com;
set $my_host $http_host;
replace $my_host .vpn. .local.
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $my_host;
proxy_set_header X-NginX-Proxy true;
proxy_pass http://10.8.25.102:8100/;
proxy_redirect http://10.8.25.102:8100/ http://$server_name/;
}
}
只是我还没有找到 replace $my_host .vpn. .local.
的正确语法我真的不关心多个替换...我不会有 a.vpn.a.vpn.domain.com
终于想通了,我可以
if ($http_host ~ ^(.*)\.vpn\.(.*)$) {
set $my_host .local.;
}
然后,由于有 CSRF 验证,我还需要重写 Referer
...所以这就是它最终的样子
server {
listen 80;
server_name *.vpn.domain.com;
set $my_host $http_host;
if ($http_host ~ ^(.*)\.vpn\.(.*)$) {
set $my_host .local.;
}
set $referer $http_referer;
set $referer_host no;
if ($http_referer ~ ^(https?://)([^/]+)(/.*)$) {
set $referer_host ;
set $rewritten_referer $my_host;
}
if ($referer_host = $http_host) {
set $referer $rewritten_referer;
}
location / {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $my_host;
proxy_set_header X-NginX-Proxy true;
proxy_set_header Referer $referer;
proxy_set_header IS_SECURE no;
proxy_pass http://10.8.25.102:8100/;
proxy_redirect https://$my_host/ https://$http_host/;
proxy_redirect http://$my_host/ http://$http_host/;
}
}