GitHub图片不能正常显示的解决办法

问题

Github作为全球最大的开源项目托管平台,国内使用频率相当高,由于种种原因,访问速度慢不说,图片还经常不能正常显示。
在这里插入图片描述

原因

图片不能访问是因为图片URL中的域名 dns 解析失败。

解决方法

找到域名的对应IP

查找域名对应IP的方法比较多,可以使用pingnslookup等命令,当然最简单的还是通过IP查询网站。
强烈建议不要用国内查询网站,有些域名不能查询!
建议使用https://www.ipaddress.com
例如:user-images.githubusercontent.com的查询结果为199.232.96.133
在这里插入图片描述

修改hosts设置静态映射

获取到域名对应的IP之后,不要直接用IP替换域名在预览器中打开,由于域名配置和证书等因素导致不能正常访问。
正确的方式为修改hosts设置静态映射,即手工设置域名的对应IP。
Windows中hosts文件路径为:C:\Windows\System32\drivers\etc\hosts
使用文本编辑器,比如记事本打开hosts文件,将以下内容添加到文件尾部,并保存。
注意:Windows中hosts文件默认只读,需要使用管理员权限进行修改。

140.82.113.4      github.com
140.82.114.10      nodeload.github.com
140.82.113.5      api.github.com
140.82.114.10      codeload.github.com

185.199.108.153      training.github.com
185.199.108.153      assets-cdn.github.com
185.199.108.153      documentcloud.github.com
185.199.108.154      help.github.com
185.199.108.153      githubstatus.com

199.232.69.194      github.global.ssl.fastly.net
199.232.96.133      camo.githubusercontent.com
199.232.96.133      raw.github.com
199.232.96.133      raw.githubusercontent.com
199.232.96.133      cloud.githubusercontent.com
199.232.96.133      gist.githubusercontent.com
199.232.96.133      marketplace-screenshots.githubusercontent.com
199.232.96.133      repository-images.githubusercontent.com
199.232.96.133      user-images.githubusercontent.com
199.232.96.133      desktop.githubusercontent.com

199.232.96.133      avatars.githubusercontent.com
199.232.96.133      avatars0.githubusercontent.com
199.232.96.133      avatars1.githubusercontent.com
199.232.96.133      avatars2.githubusercontent.com
199.232.96.133      avatars3.githubusercontent.com
199.232.96.133      avatars4.githubusercontent.com
199.232.96.133      avatars5.githubusercontent.com
199.232.96.133      avatars6.githubusercontent.com
199.232.96.133      avatars7.githubusercontent.com
199.232.96.133      avatars8.githubusercontent.com

刷新DNS

为确保修改的hosts文件生效,最好使用ipconfig /flushdns清除 DNS 解析缓存。

原理

用户访问URL时DNS解析简单流程为:

  1. 浏览器先检查自身浏览器缓存中是否有该域名的记录,即域名对应的IP地址。
  2. 检查操作系统缓存(hosts文件)中是否有对应的已解析过的结果。
  3. 请求本地域名服务器(LDNS)来解析域名,本地域名服务器可以理解为网络运营商的服务器。
  4. 如果LDNS仍然没有命中,就直接到根域名服务器请求解析。

因此,修改hosts文件就避免了DNS污染。
由于Github域名对应的IP可能会变,因此如果不能访问需要按以上步骤查询最新的IP地址更新hosts文件。

猜你喜欢

转载自blog.csdn.net/mighty13/article/details/113761084
今日推荐