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

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

问题描述

我已经在Amazon ec2上为URL www.example1.com配置了nginx.我需要将www.example1.com/blog代理传递给我的博客托管主机www.example2.com/blog,该主机托管在bluehost(切细托管)上,而无需更改浏览器中的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.

推荐答案

我将在这里给出答案.

您遇到的问题是因为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的原因,我猜是因为您没有在server_name指令中放入值example1.com.

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天全站免登陆