# 代理自动配置（Proxy auto-config）

代理自动配置文件（Proxy Auto-Configuration，PAC）是一个名为 `FindProxyForURL(url, host)` 的 [JavaScript](https://www.edgepedia.org/zh/javascript) 函数，网页浏览器会对每个请求求值该函数，以决定请求是直接发往目的地还是转发给代理服务器。它涵盖 HTTP、HTTPS 和 FTP 请求，是显式正向代理部署背后的机制——在这类部署中，流量调度必须因目标主机、域或地址而异。<sup>[1](https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/Proxy_servers_and_tunneling/Proxy_Auto-Configuration_PAC_file)</sup><sup> • </sup><sup>[2](https://learn.microsoft.com/en-us/entra/global-secure-access/concept-proxy-automatic-configuration-files)</sup>

| 关键事实 | 详情 |
|---|---|
| 核心函数 | `FindProxyForURL(url, host)`，以 JavaScript 编写，由浏览器对每个请求求值<sup>[1](https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/Proxy_servers_and_tunneling/Proxy_Auto-Configuration_PAC_file)</sup> |
| 返回值 | 一个字符串，例如 `"PROXY w3proxy.mozilla.org:8080; DIRECT"`；指令从左到右依次尝试，因此某个代理失败后会回退到下一项<sup>[1](https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/Proxy_servers_and_tunneling/Proxy_Auto-Configuration_PAC_file)</sup> |
| 起源 | “Navigator Proxy Auto-Config File Format”（Navigator 代理自动配置文件格式），在 Netscape Navigator 和 Microsoft Internet Explorer 中实现<sup>[3](https://www.rfc-editor.org/rfc/rfc3040.html)</sup> |
| 文件与 MIME 类型 | 通常以 `.pac` 扩展名保存在 Web 服务器上；服务器应使用 `application/x-ns-proxy-autoconfig` 或 `application/x-javascript-config`<sup>[4](https://assets.publishing.service.gov.uk/media/57a08c1740f0b652dd0010dc/bwmo-print.pdf)</sup><sup> • </sup><sup>[5](https://chromium.googlesource.com/experimental/chromium/src/%2B/refs/tags/83.0.4089.2/net/docs/proxy.md)</sup> |
| 自动发现 | Web 代理自动发现（Web Proxy Auto-Discovery，WPAD）协议通过 DHCP 和/或 DNS 定位 PAC URL<sup>[6](https://learn.microsoft.com/en-us/windows/win32/winhttp/winhttp-autoproxy-support)</sup> |
| HTTPS 隐私行为 | Chrome 在将 `https://` URL 传给 PAC 脚本之前会剥去其中的路径和查询串；从 Chrome 75 起无法禁用此行为<sup>[1](https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/Proxy_servers_and_tunneling/Proxy_Auto-Configuration_PAC_file)</sup> |
| 失败行为 | 在 Chromium 中，如果 PAC 脚本在执行 `FindProxyForURL()` 期间失败，代理解析会回退为直连<sup>[7](https://www.chromium.org/developers/design-documents/network-stack/proxy-settings-fallback/)</sup> |

## 工作原理

当浏览器通过显式 URL 或自动发现配置了 PAC 文件后，它会对每个请求的资源调用 `FindProxyForURL`。该函数评估目标地址、协议或主机名等条件，并返回一个指令字符串：`"DIRECT"` 表示直连，`"PROXY host:port"` 表示使用代理。由于返回值可以用分号分隔列出多条指令，一份配置可以指定一个主代理，并在首选不可用时回退到另一个代理或直连；在 MDN 的示例中，如果 `w3proxy.mozilla.org:8080` 宕机，连接会自动转为直连。<sup>[1](https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/Proxy_servers_and_tunneling/Proxy_Auto-Configuration_PAC_file)</sup><sup> • </sup><sup>[2](https://learn.microsoft.com/en-us/entra/global-secure-access/concept-proxy-automatic-configuration-files)</sup>

函数体是普通的 JavaScript：`if` 语句、字符串操作，以及一组内置辅助函数。最常用的辅助函数有：`dnsDomainIs()` 用于域匹配，`isInNet(host, pattern, mask)` 用于将 IP 地址与点分隔的模式和掩码进行匹配，`shExpMatch()` 用于 shell 风格的通配符匹配，`isPlainHostName()` 用于判断不带域的名称，`localHostOrDomainIs()` 用于精确的主机或域匹配，`isResolvable()` 用于检查 DNS 可达性，以及 `dnsResolve()`——它将主机名解析为点分 IP 地址字符串。`isInNet` 会自行将传入的主机名解析为 IP 地址，掩码中为 255 的八位组表示匹配，为 0 表示忽略。<sup>[1](https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/Proxy_servers_and_tunneling/Proxy_Auto-Configuration_PAC_file)</sup><sup> • </sup><sup>[2](https://learn.microsoft.com/en-us/entra/global-secure-access/concept-proxy-automatic-configuration-files)</sup>

一份最小配置将本地主机直连，其余全部走一个代理：

```js
function FindProxyForURL(url, host) {
  if (isPlainHostName(host) || dnsDomainIs(host, ".mozilla.org")) {
    return "DIRECT";
  }
  return "PROXY w3proxy.mozilla.org:8080; DIRECT";
}
```

MDN 称这是单代理场景下最简单、最高效的自动配置文件。<sup>[1](https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/Proxy_servers_and_tunneling/Proxy_Auto-Configuration_PAC_file)</sup>

## 部署与发现

包含 `FindProxyForURL` 函数的文件通常以 `.pac` 为扩展名，放在本地 Web 服务器的某个目录中，再由该服务器提供给客户端。<sup>[4](https://assets.publishing.service.gov.uk/media/57a08c1740f0b652dd0010dc/bwmo-print.pdf)</sup> 客户端可以手动指向该文件（通过浏览器设置中的自动代理配置 URL 之类的选项），也可以自动获取。自动路径即 Web 代理自动发现协议（WPAD）：客户端利用 DHCP 和/或 DNS，在未被明确告知的情况下发现 PAC 文件的 URL。Windows 上的 WinHTTP 5.1 正是为此目的实现了 WPAD；Chrome 在启用“自动检测代理设置”时支持 WPAD。<sup>[6](https://learn.microsoft.com/en-us/windows/win32/winhttp/winhttp-autoproxy-support)</sup><sup> • </sup><sup>[8](https://chromium.googlesource.com/chromium/src/%2B/master/docs/proxy_auto_config.md)</sup> 微软指出，对于 [Microsoft Store](https://www.edgepedia.org/zh/microsoft-store) 应用，应用的类型决定了是否会使用从 PAC 文件获取的代理设置。<sup>[9](https://learn.microsoft.com/en-us/troubleshoot/windows-server/networking/configure-proxy-server-settings)</sup>

在提供该文件时，服务器应设置 `Content-Type` 为 `application/x-ns-proxy-autoconfig` 或 `application/x-javascript-config`。<sup>[5](https://chromium.googlesource.com/experimental/chromium/src/%2B/refs/tags/83.0.4089.2/net/docs/proxy.md)</sup>

## 典型配置

**本地域内的例外。** 属于本地域但位于防火墙之外的主机（例如某台主 Web 服务器）可以走代理，而其他本地主机直连。做法是将 `dnsDomainIs()` 与 `localHostOrDomainIs()` 组合使用；MDN 建议调整条件顺序，使 `localHostOrDomainIs()` 仅对已知属于本地域的 URL 执行，从而保持求值开销低廉。<sup>[1](https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/Proxy_servers_and_tunneling/Proxy_Auto-Configuration_PAC_file)</sup>

**基于 DNS 的路由。** 在内部 DNS 服务器只解析内部名称的环境中，PAC 文件可以将可解析的主机直连，其余全部送往代理。由于 `isResolvable()` 每次调用都会查询 DNS，MDN 建议将其排在 `isPlainHostName()` 和 `dnsDomainIs()` 等开销更低的检查之后，以便仅当这些规则无法决定结果时才查询 DNS。<sup>[1](https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/Proxy_servers_and_tunneling/Proxy_Auto-Configuration_PAC_file)</sup>

**多代理间的负载分担。** PAC 文件可以按 URL 模式在多个代理之间分配流量，这能提高它们的缓存效率，因为整个代理集群中任何文档只存一份，而不是每台服务器各存一份。在 MDN 的示例中，匹配 `*.com` 的主机走代理 1，`*.edu` 主机走代理 2，其他所有域走代理 3，每条规则还列出一台第四服务器作为热备回退；所有代理均监听 8080 端口。<sup>[1](https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/Proxy_servers_and_tunneling/Proxy_Auto-Configuration_PAC_file)</sup>

## 浏览器行为与限制

**Chrome 中的 URL 剥离。** 出于隐私考虑，Chrome 在将 `https://` URL 传给 PAC 脚本之前会剥去其路径和查询串部分，因此脚本只能依赖此类 URL 的协议、主机和端口。`PacHttpsUrlStrippingEnabled` 策略在 Chrome 52 至 73 版本中控制此行为；在 Chrome 74 中只有 `--unsafe-pac-url` 命令行标志有效；从 Chrome 75 起无法禁用路径剥离。截至 Chrome 81，剥离不适用于 HTTP URL。<sup>[1](https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/Proxy_servers_and_tunneling/Proxy_Auto-Configuration_PAC_file)</sup><sup> • </sup><sup>[10](https://storage.googleapis.com/support-kms-prod/UnNg4dLAX1rqbCwQKBkIcJVFXaLz3rRegbZF)</sup>

**失败时的回退。** Chromium 的代理解析有明确的失败模式：如果 PAC 脚本在执行 `FindProxyForURL()` 期间失败，连接会回退为直连，无论是否存在手动代理设置。这可能导致相同的代理设置在 Chromium 中与其他基于 WinInet 的应用程序（如 [Internet Explorer](https://www.edgepedia.org/zh/internet-explorer)）产生不同结果。<sup>[7](https://www.chromium.org/developers/design-documents/network-stack/proxy-settings-fallback/)</sup>

**安全 Web 代理。** Chrome 可以使用 PAC 文件选择 `HTTPS` 代理类型，通过 TLS 连接代理。其他浏览器不支持 `.pac` 文件中的 `HTTPS` 代理类型，因此 Chrome 的文档提醒：修改系统级代理配置以使用此类文件可能并不可取。<sup>[11](https://www.chromium.org/developers/design-documents/secure-web-proxy/)</sup>

**实现差异。** PAC 求值在各平台上并不统一。Chrome 的文档指出，PAC 求值器的选择会影响支持的代理协议、连接数限制、代理回退行为和性能，且 Android 的 PAC 求值器在 DNS 上会阻塞。Chrome 还提供 `--winhttp-proxy-resolver` 标志，以使用系统库进行 PAC 求值，但文档将其标注为不受支持的模式并存在已知问题，包括破坏 `chrome.proxy` 扩展 API 以及降低代理策略解释的保真度。<sup>[5](https://chromium.googlesource.com/experimental/chromium/src/%2B/refs/tags/83.0.4089.2/net/docs/proxy.md)</sup>

## 历史

该格式起源于 Netscape，名为“Navigator Proxy Auto-Config File Format”；2001 年 1 月发布的 RFC 3040《互联网 Web 复制与缓存分类法》（Internet Web Replication and Caching Taxonomy）记载其在 [Netscape Navigator](https://www.edgepedia.org/zh/netscape-navigator) 和 Microsoft Internet Explorer 中实现。<sup>[3](https://www.rfc-editor.org/rfc/rfc3040.html)</sup> 原始的 Netscape 规范描述了将 JavaScript 函数保存为 `.pac` 扩展名的文件，并配置服务器的 MIME 类型，以便浏览器正确接收。<sup>[12](https://homeworks.it/Pdf/Proxy%20Automatic%20Configuration.pdf)</sup> 后来，通过 WPAD 的自动发现成为客户端在网络变化时无需手动重新配置即可找到 PAC 文件的标准方式；启用 WPAD 后，不知道 PAC URL 的浏览器会向 DHCP 服务器询问该 URL。<sup>[6](https://learn.microsoft.com/en-us/windows/win32/winhttp/winhttp-autoproxy-support)</sup><sup> • </sup><sup>[13](https://www.microsoft.com/en-us/research/wp-content/uploads/2016/02/pbp-final-with-update.pdf)</sup>

## References

1. [Proxy Auto-Configuration (PAC) file, MDN Web Docs](https://developer.mozilla.org/en-US/docs/Web/HTTP/Guides/Proxy_servers_and_tunneling/Proxy_Auto-Configuration_PAC_file)
2. [Introduction to proxy automatic configuration (PAC) files, Microsoft Learn](https://learn.microsoft.com/en-us/entra/global-secure-access/concept-proxy-automatic-configuration-files)
3. [RFC 3040: Internet Web Replication & Caching Taxonomy, IETF](https://www.rfc-editor.org/rfc/rfc3040.html)
4. [Best Practice Document: traffic filtering system (GEANT/AMRES)](https://assets.publishing.service.gov.uk/media/57a08c1740f0b652dd0010dc/bwmo-print.pdf)
5. [Proxy support in Chrome, Chromium source documentation](https://chromium.googlesource.com/experimental/chromium/src/%2B/refs/tags/83.0.4089.2/net/docs/proxy.md)
6. [WinHTTP AutoProxy Support, Microsoft Learn](https://learn.microsoft.com/en-us/windows/win32/winhttp/winhttp-autoproxy-support)
7. [Proxy settings and fallback, Chromium design documents](https://www.chromium.org/developers/design-documents/network-stack/proxy-settings-fallback/)
8. [Proxy Auto Config Using WPAD, Chromium documentation](https://chromium.googlesource.com/chromium/src/%2B/master/docs/proxy_auto_config.md)
9. [Configure proxy server settings, Microsoft Learn](https://learn.microsoft.com/en-us/troubleshoot/windows-server/networking/configure-proxy-server-settings)
10. [Chrome PAC URL stripping change notice, Google support](https://storage.googleapis.com/support-kms-prod/UnNg4dLAX1rqbCwQKBkIcJVFXaLz3rRegbZF)
11. [Secure Web Proxy, Chromium design documents](https://www.chromium.org/developers/design-documents/secure-web-proxy/)
12. [Navigator Proxy Auto-Config File Format, Netscape specification](https://homeworks.it/Pdf/Proxy%20Automatic%20Configuration.pdf)
13. [Product vulnerabilities manuscript including WPAD analysis, Microsoft Research](https://www.microsoft.com/en-us/research/wp-content/uploads/2016/02/pbp-final-with-update.pdf)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 30, 2026; Oct 15, 2026 · Edited: Sep 30, 2026; Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 网络与安全 › HTTP and web communication protocols

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/proxy-auto-config

English original: https://www.edgepedia.org/proxy-auto-config
