Temporary failure in name resolution

Temporary failure in name resolution

在谷歌中搜索标题内容可以搜到大把到内容,大概到意思就是dns配置错误,域名解析不了等等等等。

但是能把问题直接定位到这个简单到标题到人才是真正能解决问题到人。

小猪前些天就对着这些报错不知所措:

[2017-09-10 15:26:16,478] ERROR in app: Exception on /from_wechat/ [GET]
Traceback (most recent call last):
File “/usr/lib/python2.7/site-packages/flask/app.py”, line 1988, in wsgi_app
response = self.full_dispatch_request()
File “/usr/lib/python2.7/site-packages/flask/app.py”, line 1641, in full_dispatch_request
rv = self.handle_user_exception(e)
File “/usr/lib/python2.7/site-packages/flask/app.py”, line 1544, in handle_user_exception
reraise(exc_type, exc_value, tb)
File “/usr/lib/python2.7/site-packages/flask/app.py”, line 1639, in full_dispatch_request
rv = self.dispatch_request()
File “/usr/lib/python2.7/site-packages/flask/app.py”, line 1625, in dispatch_request
return self.view_functionsrule.endpoint
File “./app.py”, line 121, in from_wechat
users = oauth.get_user_info(f[‘openid’], f[‘access_token’])
File “/usr/lib/python2.7/site-packages/wechatpy/oauth/init.py”, line 198, in get_user_info
‘lang’: lang
File “/usr/lib/python2.7/site-packages/wechatpy/oauth/init.py”, line 97, in _get
**kwargs
File “/usr/lib/python2.7/site-packages/wechatpy/oauth/__init__.py”, line 66, in _request
**kwargs
File “/usr/lib/python2.7/site-packages/requests/api.py”, line 58, in request
return session.request(method=method, url=url, **kwargs)
File “/usr/lib/python2.7/site-packages/requests/sessions.py”, line 502, in request
resp = self.send(prep, **send_kwargs)
File “/usr/lib/python2.7/site-packages/requests/sessions.py”, line 612, in send
r = adapter.send(request, **kwargs)
File “/usr/lib/python2.7/site-packages/requests/adapters.py”, line 504, in send
raise ConnectionError(e, request=request)
ConnectionError: HTTPSConnectionPool(host=’api.weixin.qq.com’, port=443): Max retries exceeded with url: /sns/userinfo?access_token=tDRfd-qj5hotL-z_G2p6GXJO59bJNJnjSKPzxwrKPPAcasG9lyrOwz_0Xjc3nNURH2-xVWqo3BOc7GibiuR2GA&openid=o9S5rwPyYu8KGqPscDV1KHQjPbsQ&lang=zh_CN (Caused by NewConnectionError(‘<urllib3.connection.VerifiedHTTPSConnection object at 0x3d30090>: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution’,))
{address space usage: 289845248 bytes/276MB} {rss usage: 39464960 bytes/37MB} [pid: 6081|app: 0|req: 12/57] 117.136.66.105 () {40 vars in 955 bytes} [Sun Sep 10 15:25:45 2017] GET /from_wechat/?code=0136ql0w1hGt3d0hGf1w1gno0w16ql00&state= => generated 291 bytes in 30664 msecs (HTTP/1.1 500) 2 headers in 84 bytes (1 switches on core 0)

一开始看到这些个报错真是焦头烂额,最关键到问题还是这些错误不是每次都发生,有一定的概率会发生。这就很难办了……

使用机器来ping外网,发现有一定的机率出现unknown hostname的错误。但不是每次都出现。

仔细分析上面的错误,是由两部分组成,第一部分为python的报错(显示调用堆栈的),第二部分为uwsgi的访问log(http状态码为500了)。

而最重要的是ConnectionError这一段,去除我们项目中用到到域名,参数啥的就只剩下:

requests.exceptions.ConnectionError: HTTPSConnectionPool(host, port): Max retries exceeded with url:(Caused by NewConnectionError(urllib3.connection.VerifiedHTTPSConnection object at: Failed to establish a new connection: [Errno -3] Temporary failure in name resolution))

定位到问题以后就可以解决问题,原来公司用到虚拟机都是原生都centos系统,默认使用的dns解析服务器为谷歌提供的8.8.8.8 而国内特定的地方访问这个IP是极其不稳定,所以导致我们的程序在需要调用外部第三方API的时候可能出现解析不了第三方域名的情况,从而就出现了上面的报错。

找到linux系统的dns解析设置文件:/etc/resolv.conf文件,将原来的nameserver 8.8.8.8改成国内比较稳定的dns解析ip,这里小猪推荐使用腾讯的:

上述两个IP中,第一个为主要使用,第二个为备用。

保存之后再ping外网域名看看,是否正常了呢?

自此,本次都故障就算排除了,不过还有一个问题,就是在服务器重启或者网络服务器重启之后,我们刚修改都/etc/resolv.conf文件就会自动被修改为默认到值,所以如果想要永久使用固定到域名解析服务器,那就需要再单独设置了。