nginx 代理传递给外部 url [英] nginx proxy pass to external url

查看:56
本文介绍了nginx 代理传递给外部 url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在 amazon ec2 上为网址 www.example1.com 配置了我的 nginx.我需要将 www.example1.com/blog 代理传递给我的博客主机 www.example2.com/blog,该主机托管在 bluehost(shred 托管)上,而无需更改浏览器中的 url.可能吗?

I have configured my nginx on amazon ec2 for the url www.example1.com . I need to proxy pass www.example1.com/blog to my blogging host www.example2.com/blog which is hosted on bluehost ( shred hosting ) without changing the url in browser. Is it possible ?

我尝试了很多不同的组合,比如

I tried many different combinations like

     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 而是域名.

I could see in the log that nginx is trying to map to the IP instead of the domain which is the reason for the failure as shared hosting cannot recognize the ip but domain name.

任何输入/帮助将不胜感激.

Any input/help will be really appreciated.

推荐答案

我会在这里给出我的答案.

I'll give my answer here.

您遇到的问题是因为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

The problem you meet is because $http_host in proxy_set_header Host $http_host; uses the host in your original request header, but what you really need is the host for www.example2.com. $proxy_host will use the host in your proxy_pass directive. see Embedded Variables at the bottom http://nginx.org/en/docs/http/ngx_http_proxy_module.html

$proxy_host

$proxy_host

proxy_pass 指令中指定的代理服务器的名称和端口;

name and port of a proxied server as specified in the proxy_pass directive;

它不适用于 example1.com 而是 www.example1.com 我猜是因为你没有输入 example1 的值.comserver_name 指令中.

And the reason it is not working for example1.com but www.example1.com I guess is because you didn't put the value example1.com in server_name directive.

这篇关于nginx 代理传递给外部 url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆