How to optimize and improve a proxy.pac file
How do you optimize or improve a proxy.pac file? Make it free of errors in every engine, safe when something breaks, fast, easy for the next person to read, and no longer than it needs to be. This checklist is sorted by those five goals. Each point is short and links to the page or rule that explains it in full.
The opposite view, the mistakes that most often break routing, is on the page with the 20 most common PAC and wpad.dat mistakes. If you are new to PAC files, start with what a PAC file is. A complete file that follows every point below is the annotated reference proxy.pac.
How do I make the file free of errors?
A PAC file that fails does not show an error to the user. The browser simply connects directly, so mistakes stay hidden.
- Write ES5 JavaScript only. ES5 is the 2009 edition of JavaScript. Windows understands about the
2017 edition, and older engines fail even on
letandconst; for them the whole file is a syntax error. Newer syntax and newer library methods fail in the same way, as the page on PAC files on Windows shows. - Save the file as plain ASCII text, in UTF-8 or ASCII and not in UTF-16. Typographic quotes from a word processor are a syntax error.
- Lower-case the host once, with
host = host.toLowerCase();as the first line, and write every pattern in lower case. Chrome, Edge and Firefox already pass the host in lower case (lab, 2026-10-15), but Windows services and other programs that use WinHTTP pass it as it was typed (lab, Windows 11, 2026-10-11). - Match on
host, not onurl. Whaturlcontains differs between engines, and the path and query are chosen by whoever writes the link. - Write IP addresses as four plain decimal numbers, such as
10.1.2.3. Forms such as010.1.2.3or0x0a.1.2.3are read differently by the engines and by people. - Leave out
alert(),console.log()and objects from web pages or Node.js. - Test the file in every engine your users run, because Chromium, Firefox, pacparser and Windows can disagree; the page on how to test a PAC file shows how. Before every change, let a script compare the answers for your important URLs with the expected ones (testing in CI with pactester).
How do I make the file safe?
“Fail open” means that an error lets traffic out without the proxy. These points keep the file from failing open and keep look-alike names out.
- End the file with an unconditional default route as the last statement.
- Use a second proxy as fallback, not
DIRECT."PROXY p1:8080; DIRECT"sends everything directly while the proxy is down. If a direct fallback is intended, say so in a comment. The rules on a missing second proxy and onDIRECTas fallback explain both cases. - Do not use a
try/catchthat returnsDIRECT, because it turns every error into a bypass. - Match domains with a leading dot: write
dnsDomainIs(host, ".corp.example")and*.corp.example, nevercorp.examplewithout the dot orcorp.*. Without the dot,dnsDomainIsand a leading*match look-alike domains, and a trailing*matches any ending an attacker chooses. - Match IP ranges with
isInNeton IP addresses, not with text prefixes such as10.*. - Serve the file over HTTPS with the type
application/x-ns-proxy-autoconfigand status200. Over plain HTTP, anyone on the network path can rewrite it. WPAD discovery is always plain HTTP; the WPAD page explains the risks and how to reduce them. - Block direct internet access at the firewall. A client can ignore the PAC file, so it is not a security control on its own. With a firewall that only lets clients out through the proxy, a broken PAC file shows up as an error instead of as uncontrolled access. Whether a PAC file is secure at all, and where it is still the right tool, is explained on the page should you still use a PAC file.
How do I make the file fast?
- Decide by the host name first with
dnsDomainIs,shExpMatchand==, and put these cheap tests before anything that can touch DNS. - Call
isInNetonly on IP addresses. With a host name,isInNet,dnsResolveandisResolvablelook the name up. Only the first request to a name waits, because the engines cache the answer; when DNS does not answer, every request waits, about 12 seconds per call on Windows. A test such as/^\d+\.\d+\.\d+\.\d+$/.test(host)in front ofisInNetavoids the lookup. - Do not route by
isResolvableormyIpAddress(). The article on DNS in PAC files has the measurements for each engine. - Serve the file with an explicit cache lifetime of
a day or less, such as
Cache-Control: max-age=3600, and compress a large file. Chrome and Firefox do not use the HTTP cache for the PAC file; the header matters for other clients and for caches on the way. The page on resetting the PAC cache explains what each client keeps.
How do I make the file easy to read and support?
- Put a version and an owner in the first comment lines. When a user reports a problem, you can then ask which version their browser has.
- Comment the default route, because it decides every request that no rule matched (why it needs a comment).
- Keep the proxy string in one place, so that a change of proxy is one edit.
- Use braces on every
if. - Keep the file in version control and change it through review, like code.
How do I keep only the entries the file needs?
- List your real short names instead of sending every name without a dot direct with
isPlainHostName. Then a mistyped public name reaches the proxy and its logs. - Remove conditions that repeat.
- Give each exception a comment with its reason, so the next person can tell whether it is still needed. Exceptions without a reason are never removed.
- Keep the file small and flat. A file with hundreds of rules or deep nesting is slow and hard to review.
Check the file against this list
Paste the file into the PAC file tester. It runs the file in the engine profile you choose
(Chromium, Firefox, pacparser, or one of the two Windows profiles, WinHTTP and WinINet) and grades it
against the rule catalogue; a file that follows every point on this page gets an A+. Paste the
output of curl -sI https://pac.corp.example/proxy.pac next to the file to grade how it is served. To start
a new file instead, use the PAC file generator or one of the PAC file examples.
Frequently asked questions
How do I optimize a proxy.pac file?
Write it in plain ES5 JavaScript and end it with an unconditional default route to two proxies, so it runs without errors everywhere and does not fail open. Decide by the host name and call isInNet only on IP addresses, so DNS does not slow it down. Lower-case the host once, match domains with a leading dot, comment the default route, keep the proxy string in one place, and remove entries nobody needs. Then paste it into the tester at findproxyforurl.net/check/, which grades it against every point.
Are the best practices for wpad.dat different from those for proxy.pac?
The file itself follows the same rules, because a wpad.dat is a PAC file. Only the delivery differs. WPAD discovery fetches the file over plain HTTP from a name that anyone on the network path may be able to answer, so own every wpad name in your DNS suffixes or, better, configure the PAC URL explicitly.
Should a PAC file end with DIRECT as a fallback?
Only if uninspected traffic during a proxy outage is acceptable. A second proxy as fallback keeps traffic inspected. If you do keep DIRECT, say so in a comment so the next person knows it is intended.
May I write an IP address with leading zeros or in hex in a PAC file?
Do not. 010.1.2.3, 0x0a.1.2.3 and 167838211 are read differently by the browsers, by pactester and by Windows; a leading zero can mean octal, so 010 is 8. Even where it works, the form is confusing and leads to mistakes. Write four plain decimal numbers such as 10.1.2.3.