此文章发布于70个月前,部分信息可能已经过时,请自行斟酌确认。
需求描述
在做反向代理将请求转发到后端有时需要将用户请求的原始主机名即域名一起转给后端,下面列出 Apache、Nginx、IIS 3 种常用的 web 服务器的设置方法。
解决方案
1、apache
在 <VirtualHost/> 标签中的最后添加 ProxyPreserveHost on
<VirtualHost *:80>
RewriteEngine on
ProxyPreserveHost on
</VirtualHost>2、nginx
在 location 中添加 proxy_set_header Host $host;
location ^~/proxy_path/ {
root "/www/html";
index index.html;
proxy_pass http://192.168.10.10/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}3、iis
设置 preserveHostHeader:true
%windir%\system32\inetsrv\appcm d.exe set config -section:system.webServer/proxy -preserveHostHeader:true /commit:apphost