nginx代理proxy_pass无法使用/etc/hosts,502报错
  u1tVh2C2rGyU 2023年11月02日 39 0

在项目总遇到这样一个需求:需要将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服务器上解析


解决方法:

 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

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月08日 0

暂无评论

推荐阅读
  jnZtF7Co41Wg   2023年12月11日   31   0   0 nginx客户端服务端
  COAWCwhiWpsR   2023年12月10日   28   0   0 DNS
  COAWCwhiWpsR   2023年12月06日   30   0   0 DNS
  stLBpDewCLT1   2023年12月08日   30   0   0 nginx
  YKMEHzdP8aoh   2023年12月11日   63   0   0 DNSidePod
  jnZtF7Co41Wg   2023年12月10日   22   0   0 nginx客户端服务端NFS
  eHipUjOuzYYH   2023年12月06日   29   0   0 nginxHTTP
  eHipUjOuzYYH   2023年12月06日   24   0   0 nginx加载IPV6