跳转到内容
Guides 2 分钟阅读 826 次浏览

wget 代理设置

通过我们的完整指南掌握 wget 的代理配置。学习如何为 wget 设置并使用代理服务器,实现安全下载。

wget 代理设置

要让 wget 通过代理下载,请设置 http_proxyhttps_proxyftp_proxy 环境变量,通过命令行选项指定代理信息,或在 .wgetrc 配置文件中定义它们。

当防火墙限制了直接的互联网访问、需要将流量路由到特定网络,或出于匿名化目的时,就需要为 wget 配置代理。wget 对目标资源和代理连接都支持 HTTP、HTTPS 和 FTP 协议。

代理配置方法

配置 wget 使用代理主要有三种方法:环境变量、命令行选项和 .wgetrc 配置文件。每种方法的作用范围和持久性各不相同。

环境变量

环境变量提供系统级或会话级的代理配置,wget 会自动检测。对于临时或用户级设置,这通常是最常用的方法。

  • http_proxy:用于 HTTP 目标。
  • https_proxy:用于 HTTPS 目标。
  • ftp_proxy:用于 FTP 目标。
  • no_proxy:指定应绕过代理的域名或 IP 地址列表,以逗号分隔。

代理 URL 的通用格式为 http://[user:password@]host:port/。无论目标 URL 是 HTTP 还是 HTTPS,wget 主要使用 HTTP 代理来获取内容。因此,除非需要专门的 HTTPS 代理,http_proxy 通常对 HTTP 和 HTTPS 目标都够用。

临时配置(当前会话)

在调用 wget 之前,在 shell 中设置这些变量。

Linux/macOS(Bash/Zsh):

export http_proxy="http://proxy.example.com:8080"
export https_proxy="http://proxy.example.com:8080"
export ftp_proxy="http://proxy.example.com:8080"
export no_proxy="localhost,127.0.0.1,.internal.domain.com"

wget http://example.com/file.zip
wget https://secure.example.com/data.tar.gz

对于需要认证的代理:

export http_proxy="http://user:[email protected]:8080"
export https_proxy="http://user:[email protected]:8080"

wget http://example.com/file.zip

注意:在对安全性敏感的环境中,通常不建议将凭据直接嵌入环境变量或 URL,因为它们可能被其他进程看到,或被保存在 shell 历史记录中。

在特定会话中禁用代理:

unset http_proxy
unset https_proxy
unset ftp_proxy

Windows(CMD):

set http_proxy=http://proxy.example.com:8080
set https_proxy=http://proxy.example.com:8080
set ftp_proxy=http://proxy.example.com:8080
set no_proxy=localhost,127.0.0.1,.internal.domain.com

wget http://example.com/file.zip

Windows(PowerShell):

$env:http_proxy="http://proxy.example.com:8080"
$env:https_proxy="http://proxy.example.com:8080"
$env:ftp_proxy="http://proxy.example.com:8080"
$env:no_proxy="localhost,127.0.0.1,.internal.domain.com"

wget http://example.com/file.zip
永久配置(用户级)

要让代理设置在特定用户的各个会话之间保持有效,请将 export 命令添加到 shell 的配置文件中(例如 Linux/macOS 上的 ~/.bashrc~/.zshrc~/.profile),或添加到系统级配置文件中。

~/.bashrc 示例:

# Proxy Settings
export http_proxy="http://proxy.example.com:8080"
export https_proxy="http://proxy.example.com:8080"
export ftp_proxy="http://proxy.example.com:8080"
export no_proxy="localhost,127.0.0.1,.internal.domain.com"

编辑后,重新加载该文件或重启 shell:source ~/.bashrc

命令行选项

wget 提供了专门的代理配置命令行选项,在单次调用中会覆盖环境变量和 .wgetrc 设置。这适用于一次性下载,或需要动态控制代理设置的脚本场景。

  • --proxy-on:显式启用代理。
  • --proxy-off:显式禁用代理。
  • --no-proxy:应绕过代理的域名列表,以逗号分隔(类似于 no_proxy 环境变量)。
  • --proxy-user=<user>:指定代理用户名。
  • --proxy-password=<password>:指定代理密码。

用法示例:

# 通过代理下载文件,覆盖所有默认设置
wget --proxy-on --proxy-user=myuser --proxy-password=mypassword http://proxy.example.com:8080 http://example.com/file.zip

# 直接下载文件,绕过已配置的代理
wget --proxy-off http://example.com/file.zip

# 使用代理,但对特定域名绕过
wget --no-proxy=internal.example.com --proxy-on http://proxy.example.com:8080 http://example.com/file.zip

使用 --proxy-user--proxy-password 时,wget 会尝试用所提供的凭据向代理进行认证。代理地址本身通常仍通过环境变量或 .wgetrc 定义。

.wgetrc 配置文件

.wgetrc 文件支持持久的、用户级或系统级的代理设置。wget 在执行时会读取该文件。

  • 用户级: ~/.wgetrc(Linux/macOS),或由 WGETRC 环境变量指定的路径。
  • 系统级: /etc/wgetrc(Linux/macOS)。

~/.wgetrc 中的设置优先于 /etc/wgetrc。命令行选项会覆盖两者。

配置指令:
# Enable or disable proxy usage by default
use_proxy = on

# Define HTTP proxy
http_proxy = http://proxy.example.com:8080/

# Define HTTPS proxy
https_proxy = http://proxy.example.com:8080/

# Define FTP proxy
ftp_proxy = http://proxy.example.com:8080/

# Proxy authentication credentials
proxy_user = myuser
proxy_password = mypassword

# Domains to bypass proxy
no_proxy = localhost,127.0.0.1,.internal.domain.com

~/.wgetrc 内容示例:

# Enable proxy usage
use_proxy = on

# Proxy server details
http_proxy = http://user:[email protected]:8080/
https_proxy = http://user:[email protected]:8080/
ftp_proxy = http://user:[email protected]:8080/

# Domains to exclude from proxy
no_proxy = localhost,127.0.0.1,dev.local

代理认证

wget 支持 basic 和 digest 两种代理认证方式。

  • 通过 URL: 如环境变量示例所示(http://user:password@host:port)。这种方式安全性较低,凭据可能出现在日志或 shell 历史记录中。
  • 通过 proxy_userproxy_password.wgetrc 中或通过命令行选项(--proxy-user--proxy-password)。从安全角度通常更可取,因为 wget 会完成认证握手,而不会在 URL 中明文暴露凭据。

配置方法对比

特性 环境变量 命令行选项 .wgetrc 文件
作用范围 会话级或用户级(通过配置文件) 单次 wget 命令调用 用户级(~/.wgetrc)或系统级(/etc/wgetrc
持久性 临时(会话)或永久(配置文件) 无(一次性) 永久
优先级 被命令行选项覆盖 最高(覆盖其他所有方法) 被命令行选项和环境变量覆盖
适用场景 通用、频繁使用、脚本编写 一次性下载、动态切换代理、测试 用户或系统的默认设置
认证 通过 URL(安全性较低) 专用选项(--proxy-user--proxy-password 专用指令(proxy_userproxy_password

常见问题排查

  • "Proxy authentication required":确认 proxy_userproxy_password 设置正确,或在代理 URL 中提供了凭据。核实用户名和密码无误。
  • "Connection refused" / "Unable to connect to proxy"
    • 确认代理主机和端口正确,且代理服务正在运行。
    • 检查客户端到代理服务器的网络连通性。
    • 防火墙(客户端或代理端)可能拦截了连接。
  • "Bad Gateway" / "HTTP Error 502":代理服务器无法连接上游服务器,或在处理请求时出错。这通常说明代理服务器本身或其互联网连接存在问题。
  • wget 未使用代理
    • 确认 .wgetrc 中有 use_proxy = on,或环境变量已正确设置并导出。
    • 确保针对目标 URL 没有生效中的冲突设置 --proxy-offno_proxy
    • 检查代理地址是否有拼写错误。
  • no_proxy 不生效:确认 no_proxy 列表格式正确(以逗号分隔,逗号前后无空格),并包含准确的域名或 IP 地址。wget 采用子串匹配,因此 .example.com 会匹配 host.example.com
  • HTTPS 目标通过 HTTP 代理失败:虽然 wget 通常能通过 HTTP 代理隧道传输 HTTPS,但某些代理或网络配置可能要求为 HTTPS 单独设置 HTTPS 代理(使用 https_proxy)或直连。如果未设置 https_proxywget 会回退到 http_proxy。请确认您的代理服务器支持 SSL/TLS 隧道。
已更新: 03.03.2026
返回分类

试用我们的代理

遍布 100+ 国家的 20,000+ 代理

support_agent
GProxy Support
Usually replies within minutes
Hi there!
Send us a message and we'll reply as soon as possible.