nginx 代理 proxy_pass /etc/hosts

在项目总遇到这样一个需求:需要将nginx作为代理使用,在nginx.conf文件中配置了proxy_pass到目标网址,如下:

proxy_pass  http://$host;(A配置)

其中域名使用了变量,并且目标域名和ip地址的对应关系保存在本机的/etc/hosts文件中,运行时报502错误,error.log 下 显示 domainname could not be resolved (3: Host not found),说明没有去读/etc/hosts(proxy_pass does not resolve DNS using /etc/hosts

但是写成下面这样就可以:

proxy_pass http://www.test.com(B配置)

说明还是有读取/etc/hosts文件

原因:

个人理解:在nginx启动时会去操作系统中的/etc/hosts 等中读取,进行解析替换,然后常驻内存中,所以B配置可行;对于A配置,由于使用了变量,所以nginx会去resolver指定的dns服务器上解析


解决方法:

 by installing dnsmasq and setting your resolver to 127.0.0.1. Basically this uses your local DNS as a resolver, but it only resolves what it knows about (among those things is your /etc/hosts) and forwards the rest to your default DNS.

参考网址:

http://stackoverflow.com/questions/8305015/when-using-proxy-pass-can-etc-hosts-be-used-to-resolve-domain-names-instead-of

http://serverfault.com/questions/240476/how-to-force-nginx-to-resolve-dns-of-a-dynamic-hostname-everytime-when-doing-p

http://blog.sina.com.cn/s/blog_57c70e190100xzsr.html


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值