(转载)
最近逛 github 时发现 readme.md 中的图片都不显示了,F12 打开控制台一看是一堆的 Failed to load resource: net::ERR_CERT_COMMON_NAME_INVALID 报错。有大佬认为 ERR_CERT_COMMON_NAME_INVALID 就是用一个错误的域名访问了某个节点的 https 资源。导致这个错误的原因,基本是这几点:dns 污染;host 设置错误;官方更新了 dns,但是 dns 缓存没有被更新,导致错误解析。
解决思路
不管属于哪个原因引起的,根据hosts最高优先级原则,主要思路就是使用本地hosts文件对网站进行域名解析,一般的DNS问题都可以通过修改hosts文件来解决,github的CDN域名被污染问题也不例外,同样可以通过修改hosts文件解决,将域名解析直接指向IP地址来绕过DNS的解析,以此解决污染问题。
获取真实IP
例如,不被显示的图片URL为 https://avatars2.githubusercontent.com/u/1?s=460&v=4,打开 IPAddress.com 这个网站,输入域名 avatars2.githubusercontent.com 进行查询。得到IP地址为:151.101.184.133,如果有其他挂掉的图片一样使用此方法进行一一映射即可。
修改HOSTS文件
各系统对应的hots文件路径:
Windows系统: C:\Windows\System32\drivers\etc\hosts
Linux系统:/etc/hosts
Android(安卓)系统: /system/etc/hosts
Mac(苹果电脑)系统: /etc/hosts
iPhone(iOS)系统:/etc/hosts修改host文件在文件末尾添加:
# GitHub
192.30.253.112 github.com
192.30.253.119 gist.github.com
185.199.108.153 assets-cdn.github.com
199.232.28.133 raw.githubusercontent.com
199.232.68.133 gist.githubusercontent.com
199.232.68.133 cloud.githubusercontent.com
199.232.68.133 camo.githubusercontent.com
199.232.28.133 avatars0.githubusercontent.com
199.232.28.133 avatars1.githubusercontent.com
199.232.28.133 avatars2.githubusercontent.com
199.232.68.133 avatars3.githubusercontent.com
199.232.28.133 avatars4.githubusercontent.com
199.232.28.133 avatars5.githubusercontent.com
199.232.28.133 avatars6.githubusercontent.com
199.232.28.133 avatars7.githubusercontent.com
199.232.68.133 avatars8.githubusercontent.comhosts 文件可能需要经常维护,图片如果再次挂掉,就需要及时更新其中的 IP 地址。