PAC file best practices
A PAC file is a text file that specifies which traffic should redirect to the proxy server. When a browser makes a web request, it consults the PAC file's FindProxyForURL() function, which evaluates the request and returns routing instructions, such as a direct connection, proxy server, or failover sequence.
The default Cloudflare PAC file follows a standard format:
function FindProxyForURL(url, host) { // No proxy for private (RFC 1918) IP addresses (intranet sites) if ( isInNet(dnsResolve(host), "10.0.0.0", "255.0.0.0") || isInNet(dnsResolve(host), "172.16.0.0", "255.240.0.0") || isInNet(dnsResolve(host), "192.168.0.0", "255.255.0.0") ) { return "DIRECT"; }
// No proxy for localhost if (isInNet(dnsResolve(host), "127.0.0.0", "255.0.0.0")) { return "DIRECT"; }
// Proxy all return "HTTPS 3ele0ss56t.proxy.cloudflare-gateway.com:443";}You can customize the PAC file ↗ and host it somewhere your browser can access.
- Make sure the directive used for the endpoint is
HTTPSand notPROXY. For example:- Correct:
return "HTTPS your-subdomain.proxy.cloudflare-gateway.com:443"; - Incorrect:
return "PROXY your-subdomain.proxy.cloudflare-gateway.com:443";
- Correct:
- You must use a PAC file instead of configuring the endpoint directly in the proxy configuration of the browser. Modern browsers do not support HTTPS proxies without PAC files.
- Use a plain text editor such as VS Code to avoid extra characters.
- If you are using PAC files for public Internet browsing (instead of only internal services), refer to Common bypass rules for domains you may need to exclude from the proxy to prevent website functionality issues.
When using authorization endpoints, you must bypass your identity provider (IdP) domains in the PAC file. This prevents authentication loops where the browser tries to authenticate with the proxy before it can reach the IdP to authenticate.
The following example PAC file is a comprehensive template that includes common IdP bypass rules. Replace the placeholder values with your configuration:
function FindProxyForURL(url, host) { // *** Identity Provider Bypass *** // CRITICAL: Bypass your IdP to prevent authentication loops // Uncomment and configure the section for your IdP:
// Okta // if (host === "your-domain.okta.com" || shExpMatch(host, "*.oktacdn.com")) { // return "DIRECT"; // }
// Microsoft Entra ID (Azure AD) // if ( // host === "login.microsoftonline.com" || // host === "aadcdn.msauth.net" || // host === "aadcdn.msftauth.net" // ) { // return "DIRECT"; // }
// Google Workspace // if ( // host === "accounts.google.com" || // shExpMatch(host, "*.gstatic.com") // ) { // return "DIRECT"; // }
// GitHub // if (shExpMatch(host, "*.github.com")) { // return "DIRECT"; // }
// *** Private Networks *** // Bypass private RFC 1918 IP addresses if ( isInNet(dnsResolve(host), "10.0.0.0", "255.0.0.0") || isInNet(dnsResolve(host), "172.16.0.0", "255.240.0.0") || isInNet(dnsResolve(host), "192.168.0.0", "255.255.0.0") ) { return "DIRECT"; }
// Bypass localhost if (isInNet(dnsResolve(host), "127.0.0.0", "255.0.0.0")) { return "DIRECT"; }
// Bypass plain hostnames (no dots) if (isPlainHostName(host)) { return "DIRECT"; }
// Bypass .local domains if (shExpMatch(host, "*.local")) { return "DIRECT"; }
// *** Cloudflare Access Logout *** // Optional: Redirect logout requests to your Access logout page // if (shExpMatch(url, "*logout*")) { // return "HTTPS your-team-name.cloudflareaccess.com/cdn-cgi/access/logout"; // }
// *** Proxy all other traffic *** return "HTTPS your-subdomain.proxy.cloudflare-gateway.com:443";}Browsers evaluate PAC files for every request. Optimizing PAC file performance is critical to avoid delays and issues in web browsing for your users.
When performing DNS resolution with dnsResolve(), store the result in a variable to reuse it across multiple checks. This avoids redundant DNS lookups:
function FindProxyForURL(url, host) { // Resolve once and reuse var hostIP = dnsResolve(host);
if (isInNet(hostIP, "10.0.0.0", "255.0.0.0")) { return "DIRECT"; }
// Reuse hostIP for additional checks if (isInNet(hostIP, "172.16.0.0", "255.240.0.0")) { return "DIRECT"; }
return "HTTPS proxy.example.com:443";}NetBIOS names (hostnames without periods) are typically internal and should bypass the proxy. Check for these first:
if (isPlainHostName(host)) return "DIRECT";JavaScript is case-sensitive. Convert hostnames to lowercase for consistent matching:
function FindProxyForURL(url, host) { // Normalize to lowercase host = host.toLowerCase(); url = url.toLowerCase();
if (shExpMatch(host, "*.example.com")) { return "DIRECT"; }
return "HTTPS proxy.cloudflare-gateway.com:443";}When using PAC files for public Internet browsing (not just internal services), you may need to bypass the proxy for certain domains to prevent website functionality issues. The following are common scenarios where your proxy may interfere with traffic.
Font APIs and static asset providers should typically bypass the proxy to prevent rendering issues:
// Bypass font providersif ( shExpMatch(host, "*.googleapis.com") || shExpMatch(host, "*.gstatic.com") || shExpMatch(host, "fonts.adobe.com")) { return "DIRECT";}Video streaming and large media downloads may perform better with direct connections:
// Bypass streaming servicesif ( shExpMatch(host, "*.netflix.com") || shExpMatch(host, "*.youtube.com") || shExpMatch(host, "*.googlevideo.com")) { return "DIRECT";}Before deploying your PAC file to all users in your organization, test it with the websites and applications your users commonly access. This helps ensure:
- Internal resources are accessible and not incorrectly routed through the proxy
- External websites are properly filtered through Gateway
- Performance is acceptable for typical usage patterns
PAC files use JavaScript syntax. A single syntax error (such as a missing closing parenthesis ) or bracket ]) will cause the entire PAC file to fail. Use a JavaScript-aware text editor to find and fix syntax errors before deployment.
If you have an issue with proxy routing, most browsers provide debugging tools to verify PAC file behavior:
Chromium-based browsers (Chrome, Edge, Brave)
- In your browser, go to
chrome://net-export/(oredge://net-export/). - Select Start Logging to Disk.
- Go to the website you want to test with the affected browser.
- Select Stop Logging.
- Open the downloaded file with netlog-viewer ↗.
- Search for your domain to see proxy resolution decisions.
Firefox
- In Firefox, go to Tools > Browser Tools > Browser Console.
- Go to the website you want to test with the affected browser.
- Look for messages related to proxy resolution.
You can also test PAC file logic directly in the console by copying your FindProxyForURL function and calling it with test URLs. For example:
TODO
Safari
- In Safari, go to Safari > Settings, then select Advanced.
- Turn on Show Develop menu in menu bar.
- Select Develop > Show Web Inspector.
- Go to the Network tab.
- Look at the request details to verify proxy usage.
Excessive DNS lookups in the PAC file can cause delays. Review your PAC file and minimize the use of dnsResolve(), isInNet(), and isResolvable() functions.
When you update a PAC file, browsers may continue to use a cached version, causing unexpected behavior. Clear your browser cache and restart the browser after updating the PAC file to ensure the browser uses the latest version.
Was this helpful?
- Resources
- API
- New to Cloudflare?
- Directory
- Sponsorships
- Open Source
- Support
- Help Center
- System Status
- Compliance
- GDPR
- Company
- cloudflare.com
- Our team
- Careers
- © 2025 Cloudflare, Inc.
- Privacy Policy
- Terms of Use
- Report Security Issues
- Trademark
-