How to set up a proxy.pac file: create it, host it, point clients to it, test it

Published · updated

Diese Anleitung auf Deutsch

A PAC file is a text file with one JavaScript function in it. Setting one up takes four steps. You write the file, put it on a web server, tell the clients where it is, and test it before and after the rollout. This guide goes through all four. Each step links to the page that has the details.

Step 1: write the file

A PAC file is one function that answers one question

For every request, the browser calls the function FindProxyForURL(url, host) in the PAC file and asks: “which proxy should I use for this request?”. url is the full address of the request, and host is only the host name from it, such as www.example.com. The function answers with a text string:

  • "DIRECT" means: connect without a proxy.
  • "PROXY proxy1.corp.example:8080" means: use this proxy.
  • "PROXY proxy1.corp.example:8080; PROXY proxy2.corp.example:8080" is a list. The browser tries the entries from left to right and takes the next one when a proxy does not answer.

That is the whole interface. The FindProxyForURL reference has the details, and the function reference lists the helper functions, such as dnsDomainIs() and isPlainHostName(), that every PAC engine provides.

Start with the default route

Open a plain-text editor, create a file named proxy.pac, and write the line that decides every request first:

function FindProxyForURL(url, host) {
  // Default route: everything goes through the corporate proxies
  return "PROXY proxy1.corp.example:8080; PROXY proxy2.corp.example:8080";
}

This is already a working PAC file. Everything you add from now on goes above this return, because the first return that runs decides the request, and the default route must stay the last statement. If the function can end without a return, browsers connect directly and the traffic silently bypasses the proxy (details).

Lower-case the host before you compare it

  host = host.toLowerCase();

This goes in as the first line of the function. Host names are not case-sensitive, but the comparisons in your file are, and not every client lower-cases the host before it calls the PAC file (details).

Send internal destinations direct

Add one if block per group of destinations, each ending in a return:

  // plain host names (no dot) are on the local network
  if (isPlainHostName(host)) {
    return "DIRECT";
  }

  // the internal DNS zone and all its hosts
  if (host == "corp.example" || dnsDomainIs(host, ".corp.example")) {
    return "DIRECT";
  }

Put the cheapest tests first. isPlainHostName() only checks whether the name contains a dot. For a domain, you need both tests: dnsDomainIs() with the leading dot matches every host inside the zone, and == matches the zone name itself.

Internal networks need care, because the obvious test isInNet(host, "10.0.0.0", "255.0.0.0") looks the name up in DNS. The first request to each name waits for the answer, and every request waits when the DNS server is slow or out of reach (DNS in PAC files). Test networks only when the URL contains an IP address, as the local bypass example does.

The complete first file

Put the pieces together and you have a file that most company networks can start with:

function FindProxyForURL(url, host) {
  host = host.toLowerCase();

  // plain host names (no dot) are on the local network
  if (isPlainHostName(host)) {
    return "DIRECT";
  }

  // the internal DNS zone and all its hosts
  if (host == "corp.example" || dnsDomainIs(host, ".corp.example")) {
    return "DIRECT";
  }

  // Default route: everything else goes through the corporate proxies
  return "PROXY proxy1.corp.example:8080; PROXY proxy2.corp.example:8080";
}

Replace corp.example and the proxy names with your own, then test the file in the PAC file checker with the URLs that matter to you. isPlainHostName keeps this first file short; a production file lists the real short names instead, so that a mistyped name reaches the proxy, as the reference proxy.pac does (PAC-B006).

Most mistakes send traffic past the proxy without an error

Most broken PAC files fail in one of a few ways, and most of them send traffic past the proxy without an error. The 20 most dangerous PAC mistakes shows how often each one appears in real files and how to fix it, and the checklist on how to optimize and improve a proxy.pac lists what to do instead. The checker finds these mistakes and explains each finding with the line it is on.

The faster ways: an example or the generator

You do not have to start from an empty file:

  • The PAC file examples are complete files for common cases: one proxy for everything, local traffic direct, a different proxy per domain, failover, SOCKS, one file per site, and the files for the common cloud proxy services. Each one scores A+ in the checker.
  • The annotated reference proxy.pac explains a production file block by block.
  • The PAC file generator builds a file from your own lists of proxies, hosts, domains and networks, with a comment next to every entry. The page also explains where to set the file’s URL on Windows, macOS and in group policy.

Step 2: put the file on a web server

Clients fetch the PAC file from a URL, so it needs a web server that every client can reach, also before the proxy is working. Most companies use a small internal web server or the proxy’s own web service. Choose a fixed address that will not change, for example https://pac.corp.example/proxy.pac, because every client will have this address in its settings.

The server must deliver the file like this:

WhatWhyRule
over https://over plain HTTP anyone on the network path can rewrite the file and send all web traffic to a proxy of their choicePAC-D001
status 200with any other status the client has no script and runs without onePAC-D010
without a loginChrome, Edge and Windows do not log in to fetch a PAC filePAC-D012
Content-Type: application/x-ns-proxy-autoconfigthe clients load the file anyway, but a wrong type usually means the server sends an error page or has no setting for the filePAC-D002
Cache-Control: max-age of a day or lessit decides how long proxies, CDNs and clients with an HTTP cache keep an old file; Chrome and Firefox fetch the file past the HTTP cache anywayPAC-D003, PAC-D005
saved as plain ASCII or UTF-8, never UTF-16Windows and Firefox cannot read a UTF-16 filePAC-D013
smaller than 1 MiBChrome, Edge and Windows ignore a larger file completelyPAC-D011

Do not serve the file from a file share or a file:// path: Chromium no longer accepts such PAC URLs, and whoever can write to the share controls the routing (PAC-D009).

On Apache, a few lines in the configuration of the site set the type and the cache time (Header needs the module mod_headers):

AddType application/x-ns-proxy-autoconfig .pac
<Files "proxy.pac">
    Header set Cache-Control "max-age=3600"
</Files>

The wpad.dat generator shows the same settings for nginx and IIS. Then check what the server really sends:

curl -sI https://pac.corp.example/proxy.pac

The first line must say 200, and the headers must contain the content type and cache-control. Paste this output into the PAC file checker under Delivery, and it grades the delivery as well.

Step 3: tell the clients where the file is

Every client needs the URL of the file. The safest way is to set it explicitly, centrally, for each kind of client:

ClientWhere the PAC URL is set
Windows 10 and 11, per userSettings > Network & internet > Proxy, Use setup script, then the script address; for many machines by Group Policy or Intune
Windows services and background programs (WinHTTP)separately, with netsh winhttp set advproxy
Chrome and Edgethey follow the Windows or macOS setting; the browser policy ProxySettings with ProxyMode pac_script and ProxyPacUrl overrides it
Firefoxits default Use system proxy settings takes the system’s PAC URL; the enterprise policy Proxy with "Mode": "autoConfig" and an AutoConfigURL sets its own
macOSSystem Settings > Network, the network service, Details > Proxies, Automatic proxy configuration; for managed Macs an MDM profile
Devices you cannot configureWPAD discovery through DHCP option 252 or the DNS name wpad, as the last choice

Where these settings land and why a rolled-out policy sometimes has no effect is explained on PAC file not applied by Group Policy or Intune.

When you set a script, switch Automatically detect settings off on the same client. If it stays on, a wpad answer on any network the laptop joins replaces your script (which setting wins).

WPAD finds the file without any setting on the client, which is why it is tempting. But discovery runs over plain HTTP and asks DHCP and DNS without any authentication, so whoever answers first can supply the file. Use it only where you cannot configure the clients; the WPAD page explains the risks and how to make it safer.

Step 4: test it before and after the rollout

Before the rollout, open the file in the PAC file checker and trace the URLs that matter to you, one per rule, in the engine profiles your users run. Then give the URL to a small pilot group before everyone gets it. The article how to test a PAC file lists which URLs to try and how to test on the command line and in the browser itself.

After the rollout, check on a client that it really uses the file. Clients that loaded an older file may keep it for a while; how to make clients reload a PAC file shows how to force a reload in each engine. When a client does not use the file at all, the PAC file troubleshooting guide goes through the symptoms.

Frequently asked questions

How do I set up a PAC file?

Write a text file named proxy.pac with a function FindProxyForURL(url, host) that returns "DIRECT" or "PROXY host:port" for each request. Serve it from a web server over HTTPS with status 200 and the content type application/x-ns-proxy-autoconfig. Give the clients its URL through the Windows setup script setting, Group Policy, Intune, the browser policies or an MDM profile, and use WPAD only for devices you cannot configure. Test the file before the rollout and again on a pilot group.

Which program do I need to write a PAC file?

Any plain-text editor. Save the file as plain ASCII without a byte order mark, and do not use a word processor, because it turns straight quotes into curly ones, which are a syntax error in JavaScript.

What is the simplest possible PAC file?

A FindProxyForURL function with a single line, return "PROXY proxy.corp.example:8080";, which sends every request to one proxy. A real file adds direct routes for internal destinations and a second proxy for failover.

Can I create a PAC file without writing code?

Yes. The PAC file generator on this site builds a commented file from your lists of proxies, hosts, domains and networks, and the examples page has complete files that you can copy and adapt.

Where do I put the PAC file?

On a web server that every client can reach, at a fixed https:// address such as https://pac.corp.example/proxy.pac. Not on a file share, because Chromium no longer loads PAC files from file paths, and not behind a login, because Chrome, Edge and Windows do not log in to fetch it.