检查Linux服务器wget下载出现403 Forbidden报错问题

一般我们在服务器中下载文件的话会用到wget命令。这里有下载报错403 Forbidden,这里需要排查问题。

1. 检查 URL 是否正确

确保你输入的 URL 拼写正确,没有遗漏或多余的字符。例如:

  1. wget http://example.com/path/to/file

2. 确认文件或目录的权限

有些资源可能对公众不可见或需要特定权限才能访问。你可以尝试在浏览器中打开该 URL,看看是否能够正常访问。如果浏览器也显示403 Forbidden,说明资源本身可能受限。

3. 使用合适的 User-Agent

有些服务器会通过检测User-Agent 来决定是否允许访问。默认情况下,wgetUser-Agent 可能会被服务器识别为爬虫或非浏览器客户端,从而拒绝访问。你可以通过设置--user-agent 参数来模拟浏览器的User-Agent

示例:

  1. wget --user-agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36" http://example.com/path/to/file

4. 处理 HTTP Referrer

有些服务器会检查请求的来源(Referrer),如果缺少或不符合要求,可能会拒绝访问。你可以使用--referer 参数来指定 Referrer。

示例:

  1. wget --referer="http://www.example.com" http://example.com/path/to/file

5. 添加 Cookies

某些网站需要特定的 Cookies 才能访问资源。你可以先在浏览器中访问该网站,获取必要的 Cookies,然后在wget 中使用--header 参数添加 Cookies。

示例:

  1. wget --header="Cookie: sessionid=YOUR_SESSION_ID" http://example.com/path/to/file

或者使用--load-cookies 参数加载包含 Cookies 的文件:

  1. wget --load-cookies cookies.txt http://example.com/path/to/file

要生成cookies.txt,可以使用工具如 http.cookiejar 或浏览器扩展导出 Cookies。

6. 使用--execute robots=off

有些网站通过robots.txt 文件限制爬虫访问。虽然wget 默认遵守robots.txt,但你可以通过--execute robots=off 参数来忽略它。

示例:

  1. wget --execute robots=off http://example.com/path/to/file

7. 增加请求头信息

有些服务器可能需要特定的请求头才能允许访问。你可以使用--header 参数添加所需的头部信息。

示例:

  1. wget --header="Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8" \
  2. --header="Accept-Language: en-US,en;q=0.5" \
  3. http://example.com/path/to/file

8. 使用--header 综合示例

结合多个参数,模拟更真实的浏览器请求:

  1. wget --user-agent="Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36" \
  2. --header="Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8" \
  3. --header="Accept-Language: en-US,en;q=0.5" \
  4. --referer="http://www.example.com" \
  5. --load-cookies cookies.txt \
  6. http://example.com/path/to/file

我们一一排查看看是什么问题。

投上你的一票

本文出处:老蒋部落 » 检查Linux服务器wget下载出现403 Forbidden报错问题 | 欢迎分享( 公众号:老蒋朋友圈 )

公众号 「老蒋朋友圈」获取站长新知 / 加QQ群 【1012423279】获取商家优惠推送