HowTo: Use Auto Config Proxy PAC File For Specific Domain

by on November 20, 2012 · 4 comments· LAST UPDATED November 20, 2012

in

I would like to use the squid proxy server only for one domain called foo.example.com. How do I auto-configuring proxy settings with a PAC file to hide one domain and connect everything else directly? How do I specify a URL in a PAC file to bypass proxy server? How do I create exceptions using a PAC file?

Tutorial details
DifficultyIntermediate (rss)
Root privilegesYes
RequirementsWeb server
Javascript skills
Estimated completion timeN/A

A PAC file is nothing but proxy auto-configuration file. This is a specialized JavaScript function definition that a browser calls to determine how requests are handled. In other words, you need the Javascript skills for most PAC file development.

Syntax

A very simple example of a PAC file is:

 
   function FindProxyForURL(url, host)
   {
      return "PROXY server1.cyberciti.biz:3128; DIRECT";
   }
 

You need to create and upload proxy.pac file in your web server DocumentRoot. This file set a basic proxy server for every request your browser makes using server1.cyberciti.biz at port 3128. Firefox, Chrome, Opera, and IE uses your computer's system proxy settings to connect to the network. However, you can change these settings by visiting network settings option. You need to supply the url for proxy.pac file. In the Automatic proxy connfiguration URL box, type the url of the proxy.pac file such as http://192.168.1.100/proxy.pac or http://server1.cyberciti.biz/proxy.pac:

Fig.01: Setting firefox pac file url

Fig.01: Setting firefox pac file url

Example

In this example, you use the proxy server for foo.example.com, and directly connect to all other site.

 
function FindProxyForURL(url, host) {
    if ( localHostOrDomainIs(host, "foo.example.com") ) {
        return "PROXY server1.cyberciti.biz:3128";
    } else {
        return "DIRECT";
    }
}
 

This proxy.pac file allows to connect various hosts / domains via the proxy server and rest hosts directly to the Internet:

 
function FindProxyForURL(url, host) {
    // Your proxy server name and port
    var proxyserver = 'server1.cyberciti.biz:3128';
    //
    //  Here's a list of hosts to connect via the PROXY server
    //
    var proxylist = new Array(
        "nixcraft.com",
        "reddit.com",
        "www.cyberciti.biz",
        "mail.google.com",
        "www.pandora.com",
        "www.google.com"
    );
    // Return our proxy name for matched domains/hosts
    for(var i=0; i<proxylist.length; i++) {
        var value = proxylist[i];
        if ( localHostOrDomainIs(host, value) ) {
            return "PROXY "+proxyserver;
        }
    }
    return "DIRECT";
}
 

You create a PAC files are easily modified to specify any number of URLs that will bypass the proxy or include in the proxy i.e. exceptions can be created using the following syntax:

 
if (shExpMatch(url, "*.slashdot.org/*"))
   {return "DIRECT";}
 

OR connect reddit.com via the proxy server:

 
if (shExpMatch(url, "*.reddit.com/*"))
   {return "PROXY proxy42.ca.cyberciti.biz:8080";}
 
TwitterFacebookGoogle+PDF versionFound an error/typo on this page? Help us!

{ 4 comments… read them below or add one }

1 petrescs November 21, 2012 at 1:15 pm

Additionally, Web Proxy Autodiscovery Protocol (WPAD) can be used http://en.wikipedia.org/wiki/Web_Proxy_Autodiscovery_Protocol – this can work either through DHCP settings or DNS lookups (wpad.dat can link to or replace proxy.pac, but content remains identic). This way there’s no need for explicit input of proxy URL, just keep default “Auto-detect proxy settings for this network”.

Reply

2 milky July 30, 2013 at 10:22 am

Hi,
do you know that package manager works with authomatic proxy conf url? I work on Debian..

Reply

3 justin September 12, 2013 at 7:17 am

What if you want a new proxy for each url – ie a new proxy everytime a new page is loaded (on the same domain)?

Reply

4 marco luis September 22, 2014 at 10:49 am

what is the proxy that I need in connecting into the Internet

Reply

Leave a Comment

Tagged as: , , , , ,

Previous Faq:

Next Faq: