NGINX proxy_pass 域名解析问题
前两天发现一个问题,当使用proxy_pass的时候,发现域名对应IP是缓存的,这样一旦VIP变化之后,就会报错,下面就来详细分析一下这个问题。
一、问题说明
location = /test {
internal;
no_error_pages;
proxy_pass_request_headers off;
proxy_pass 'http://www.taobao.com/test/router/rest';
}
大家应该知道,这是向http://www.taobao.com/test/router/rest发送请求,其实是向202.108.250.251发送请求
ping www.taobao.com
PING scorpio.danuoyi.tbcache.com (202.108.250.251): data bytes
bytes from 202.108.250.251: icmp_seq= ttl= time=4.324 ms
bytes from 202.108.250.251: icmp_seq= ttl= time=8.320 ms
如果在服务的过程中突然www.taobao.com对应的IP变化了(这是非常常见的,因为taobao.com对应多个域名),那么NGINX仍然会访问以前的IP202.108.250.251,这是为什么呐?下面咱们就来分析一下。
二、HTTP模块
打开NGINX的源代码,搜索proxy_pass,发现在文件:src/http/modules/ngx_http_proxy_module.c实现了这个方法
static char *
ngx_http_proxy_pass(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
......
}
那么,继续往下看
1、url = &value[1]; //获取url
2、ngx_memzero(&u, sizeof(ngx_url_t));
3、plcf->upstream.upstream = ngx_http_upstream_add(cf, &u, 0);
用gdb分析,发现ngx_http_upstream_add()函数在文件src/http/ngx_http_upstream.c里面。代码如下:
ngx_http_upstream_srv_conf_t *
ngx_http_upstream_add(ngx_conf_t *cf, ngx_url_t *u, ngx_uint_t flags)
............................................................
- if (ngx_parse_url(cf->pool, u) != NGX_OK) {
if (u->err) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, ,"%s in upstream \"%V\"", u->err, &u->url);
}
return NULL;
}
.............................................................
4、ngx_parse_url,它实在文件core/ngx_inet.c里面,下面是调用的顺序
- ngx_parse_url()调用ngx_parse_inet_url()
- ngx_parse_inet_url()调用ngx_inet_resolve_host()
- ngx_inet_resolve_host()调用gethostbyname()
- gethostbyname()函数就是通过域名获取IP的函数
三、问题解决
未完,待续。。。
NGINX proxy_pass 域名解析问题的更多相关文章
- nginx泛域名解析实现二级域名多域名
利用nginx泛域名解析配置二级域名和多域名 网站的目录结构为html├── bbs└── www html为nginx的安装目录下默认的存放源代码的路径. bbs为论坛程序源代码路径www为主页程序 ...
- 利用nginx泛域名解析配置二级域名和多域名
利用nginx泛域名解析配置二级域名和多域名 网站的目录结构为 html ├── bbs └── www html为nginx的安装目录下默认的存放源代码的路径. bbs为论坛程序源代码路径 www为 ...
- nginx proxy_pass 指令
nginx proxy_pass 指令 文档 Nginx 官方文档 https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pa ...
- nginx proxy_pass
在nginx中配置proxy_pass时,如果是按照^~匹配路径时,要注意proxy_pass后的url最后的/,当加上了/,相当于是绝对根路径,则nginx不会把location中匹配的路径部分代理 ...
- nginx proxy_pass 与 rewrite 简记
rewrite syntax: rewrite regex replacement [flag] Default: - Context: server, location, if 如果正则表达式(re ...
- nginx proxy_pass 代理域名
一.描述 1.nginx配置转发的时候使用的是域名,即使用dns服务方便配置和负载.但是nginx默认会进行缓存,当域名对应的服务出问题的时候就会报错,只有默认的缓存时间到了才会再次进行解析,ngin ...
- nginx proxy_pass指令’/’注意事项
1. proxy_pass配置说明 不带/ location /test/ { proxy_pass http://t6:8300; } 带/ location /test/ { proxy_pass ...
- 用反向代理nginx proxy_pass配置解决ie8 ajax请求被拦截问题 ie8用nginx代理实现跨域请求访问 nginx405正向代理request_uri
最近调PC版网站ie8的兼容性,发现所有ajax请求还没到后端服务器就直接ajax error了 ie8发不出ajax请求,断点调试发现ajax全进入了error,提示“No transport” 我 ...
- nginx proxy_pass后的url加不加/的区别
在nginx中配置proxy_pass时,当在后面的url加上了/,相当于是绝对根路径,则nginx不会把location中匹配的路径部分代理走;如果没有/,则会把匹配的路径部分也给代理走. 下面四种 ...
随机推荐
- 架构:Screaming Architecture(转载)
Imagine that you are looking at the blueprints of a building. This document, prepared by an architec ...
- SQLUnit 环境搭建
1 技术须要积累 前几天去一位笛友小赖家玩,才知道他的笛子吹得好不是偶然.是由于他真的用功了,电脑里面下载有超多的视频.教程等!这真的非常让我震撼!一直是在公司做技术,但资料考不出来,而我整理了多少技 ...
- 出现Running Android Lint的错误
进入设置,让软件不要检查即可.
- Java获取当前时间30天之前的时间
//方法一 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String maxDateStr = " ...
- [Web 前端] MobX
1. 介绍 1.1. 原理 React的render是 状态 转化为树状结构的渲染组件的方法 而MobX提供了一种存储,更新 状态 的方法 React 和 MobX都在优化着软件开发中相同的问题. R ...
- 使用Numpy验证Google GRE的随机选择算法
最近在读<SRE Google运维解密>第20章提到数据中心内部服务器的负载均衡方法,文章对比了几种负载均衡的算法,其中随机选择算法,非常适合用 Numpy 模拟并且用 Matplotli ...
- Linux Shell 裡一些很少用到卻很有用的指令
Linux Shell 裡一些很少用到卻很有用的指令 2009年11月30日 13:53:00 yaoyasong 阅读数:414 Linux Shell 裡一些很少用到卻很有用的指令 你是不是已 ...
- cross validation笔记
preface:做实验少不了交叉验证,平时常用from sklearn.cross_validation import train_test_split,用train_test_split()函数将数 ...
- PHP xhprof性能优化
xhprof window http://dev.freshsite.pl/php-extensions/xhprof.html http://php.net/manual/en/book.xhpro ...
- Commands to help you to Start Using ScaleIO Storage
To start using your storage: Log in to the MDM: scli --login --username admin --password <passwor ...