我已经在亚马逊ec2上为url www.example1.com配置了我的nginx。我需要代理传递www.example1.com/blog给我的博客主机www.example2.com/blog,它托管在bluehost ( shred托管)上,而不需要更改浏览器中的url。有可能吗?
我尝试了许多不同的组合,比如
location /blog{
proxy_pass http://www.example2.com;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;}
一点运气都没有。
我可以在日志中看到nginx试图映射到IP而不是域名,这是失败的原因,因为共享主机无法识别ip而只能识别域名。
任何输入/帮助都将非常感谢。
发布于 2016-07-14 19:16:22
我在这里给出我的答案。
您遇到的问题是因为proxy_set_header Host $http_host;中的$http_host使用了原始请求头中的主机,但您真正需要的是www.example2.com的主机。$proxy_host将在您的proxy_pass指令中使用主机。参见底部http://nginx.org/en/docs/http/ngx_http_proxy_module.html中的Embedded Variables
$proxy_host
proxy_pass指令中指定的代理服务器的名称和端口;
它不适用于example1.com而适用于www.example1.com的原因,我猜是因为您没有将值example1.com放在server_name指令中。
https://stackoverflow.com/questions/38345233
复制相似问题