博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
nginx 代理 proxy_pass /etc/hosts
阅读量:4121 次
发布时间:2019-05-25

本文共 1005 字,大约阅读时间需要 3 分钟。

在项目总遇到这样一个需求:需要将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

转载地址:http://ugspi.baihongyu.com/

你可能感兴趣的文章
Mysql中下划线问题
查看>>
微信小程序中使用npm过程中提示:npm WARN saveError ENOENT: no such file or directory
查看>>
Xcode 11 报错,提示libstdc++.6 缺失,解决方案
查看>>
idea的安装以及简单使用
查看>>
Windows mysql 安装
查看>>
python循环语句与C语言的区别
查看>>
Vue项目中使用img图片和background背景图的使用方法
查看>>
vue 项目中图片选择路径位置static 或 assets区别
查看>>
vue项目打包后无法运行报错空白页面
查看>>
Vue 解决部署到服务器后或者build之后Element UI图标不显示问题(404错误)
查看>>
element-ui全局自定义主题
查看>>
facebook库runtime.js
查看>>
vue2.* 中 使用socket.io
查看>>
openlayers安装引用
查看>>
js报错显示subString/subStr is not a function
查看>>
高德地图js API实现鼠标悬浮于点标记时弹出信息窗体显示详情,点击点标记放大地图操作
查看>>
初始化VUE项目报错
查看>>
vue项目使用安装sass
查看>>
HTTP和HttpServletRequest 要点
查看>>
在osg场景中使用GLSL语言——一个例子
查看>>