487
edits
mNo edit summary |
(Fix grammatical stuff and clarify meaning; Updated broken external link.) |
||
Line 18: | Line 18: | ||
'''PAC and Portability'''<br>'''Uniform Server 3.5-Apollo''' | '''PAC and Portability'''<br>'''Uniform Server 3.5-Apollo''' | ||
|} | |} | ||
On the previous page I covered name-based virtual hosting | On the previous page I covered name-based virtual hosting. It uses the local machine's Hosts file to resolve domain names. To run a portable development server you need to edit the new target PC’s Hosts file. This page looks at resolving this issue using a PAC file and a portable browser, making the server completely portable. | ||
PAC allows you to simulate a DNS server locally one line per CNAME entry. Unlike the hosts file it is dynamic. PAC is a standard uses a single file and is supported by all new browsers. | PAC allows you to simulate a DNS server locally, with one line per CNAME entry. Unlike the hosts file it is dynamic. PAC is a standard, uses a single file and is supported by all new browsers. | ||
Ever had problems testing sites with a mix of relative, root relative and absolute links | Ever had problems testing sites with a mix of relative, root relative and absolute links? Ever wanted to test cross-site interlinking? The following trio may be what you are looking for. Uniform Server, portable (Firefox or Opera) and PAC make an ideal test environment, all on a USB memory stick. | ||
In the light of issues raised overall Firefox portable is the best option. | In the light of issues raised overall, Firefox portable is the best option. | ||
== Background == | == Background == | ||
Testing multi-web sites is relatively easy | Testing multi-web sites is relatively easy. First create a virtual host section for each site in the Apache configuration file. Use the '''hosts''' file on your machine to resolve the web site names to IP address. That’s it; finished. Suppose you do not have access to the host file. What alternatives are there? | ||
If you use only relative links on your site you can use the Apache alias and re-map | If you use only relative links on your site, you can use the Apache alias and re-map. However if your sites are using absolute or root-relative links, this method will fail. | ||
So what other methods are there for resolving IP addresses? A local DNS server would do nicely, even allow you to enter MX records. That seems a bit of overkill, since all we want to do is convince our browser to pick up pages from a different server masquerading as the real server. Enter the world of proxy servers. | |||
'''''[[#top | Top]]''''' | '''''[[#top | Top]]''''' | ||
== PAC == | == PAC == | ||
Web browsers can be configured to use a proxy server allowing the use of files or other | Web browsers can be configured to use a proxy server, allowing the use of files or other resources available on a different server. | ||
This process is automated with a '''Proxy Auto-Configuration''' (PAC) file. Sounds complicated, not really | This process is automated with a '''Proxy Auto-Configuration''' (PAC) file. Sounds complicated, but not really. The PAC file is a simple text file containing a few instructions. | ||
Just tell your browser where to find it by setting the appropriate options. Your browser reads this file when it is re-started and whenever it needs to resolve an IP address. | Just tell your browser where to find it by setting the appropriate options. Your browser reads this file when it is re-started and whenever it needs to resolve an IP address. | ||
Any special | Any special instructions are executed '''before''' your browser attempts to resolve an IP address. This is why PAC is so powerful. | ||
'''''[[#top | Top]]''''' | '''''[[#top | Top]]''''' | ||
Line 48: | Line 48: | ||
|- | |- | ||
|valign="top"| | |valign="top"| | ||
The PAC file is a JavaScript consisting of single function. This function receives two parameters ('''url''' and '''host''').<br>Parameters are automatically provided by a browser. | The PAC file is a JavaScript consisting of a single function. This function receives two parameters ('''url''' and '''host''').<br>Parameters are automatically provided by a browser. | ||
Line 75: | Line 75: | ||
| | | | ||
The hostname extracted from the URL. It is the string between '''://''' and the first ''':''' or '''/''' after that e.g.<br><nowiki>http://</nowiki>'''wiki.uniformserver.com/index.php'''/Main_Page<br> | The hostname extracted from the URL. It is the string between '''://''' and the first ''':''' or '''/''' after that e.g.<br><nowiki>http://</nowiki>'''wiki.uniformserver.com/index.php'''/Main_Page<br> | ||
''Note 1'': The port number is not included. If required can be extracted from the URL. | ''Note 1'': The port number is not included. If required, it can be extracted from the URL. | ||
|- | |- | ||
|valign="top"| | |valign="top"| | ||
*'''ret''' | *'''ret''' | ||
|The return value a string describing the configuration. | |The return value is a string describing the configuration. | ||
|} | |} | ||
'''Return Value:''' | '''Return Value:''' | ||
The JavaScript function must return a single string | The JavaScript function must return a single string. If the string is null, no proxies will be used. The string can contain any of the following blocks, separated by a semicolon: | ||
{|cellpadding="6" | {|cellpadding="6" | ||
|- | |- | ||
Line 94: | Line 94: | ||
|} | |} | ||
There are a number of predefined functions you can use | There are a number of predefined functions you can use. We are interested in only two: '''shExpMatch''' and '''dnsDomainIs'''. | ||
If these do not suit your needs you can easily find more information on the Internet | If these do not suit your needs, you can easily find more information on the Internet. This is worth a read, and the site has more PAC info: [http://www.returnproxy.com/proxypac/static/netscape-proxy-format.html Navigator Proxy Auto-Config File Format]. | ||
'''''[[#top | Top]]''''' | '''''[[#top | Top]]''''' | ||
== Compare function == | == Compare function == | ||
This function compares two strings I think the only special character it accepts is the wildcard character * (matches one or more characters) not a limitation probably makes it easier to use. The function has this format: | This function compares two strings. I think the only special character it accepts is the wildcard character * (matches one or more characters). It's not a limitation and probably makes it easier to use. The function has this format: | ||
'''shExpMatch(str, shexp)''' | '''shExpMatch(str, shexp)''' | ||
Line 125: | Line 125: | ||
shExpMatch("<nowiki>http://home.unicenter.com/site2/index.html</nowiki>", "*site1*") is false. | shExpMatch("<nowiki>http://home.unicenter.com/site2/index.html</nowiki>", "*site1*") is false. | ||
The above is just an example | The above is just an example. Replace the fixed string with a variable such as the url, for example: | ||
(shExpMatch('''url''', "*site1*"); | (shExpMatch('''url''', "*site1*"); | ||
Line 131: | Line 131: | ||
Any url passed to the above function containing the string site1 ('''anywhere''') will produce true. | Any url passed to the above function containing the string site1 ('''anywhere''') will produce true. | ||
The result | The result can be filtered using an '''if''' statement. If true do something else continue onto the next line of code. | ||
{| cellpadding="4" cellspacing="1" style="background:#000000;" | {| cellpadding="4" cellspacing="1" style="background:#000000;" | ||
|- style="background:#e8e8e8;" | |- style="background:#e8e8e8;" | ||
Line 140: | Line 140: | ||
|} | |} | ||
When the comparison is true, the something to do is to return an IP address of a proxy server. The url our browser was looking for can be found on the proxy server '''127.0.0.1''' (that's our local test server). It's this return value that resolves the IP address hence the browser is happy and fetches the information. | |||
'''''[[#top | Top]]''''' | '''''[[#top | Top]]''''' | ||
Line 159: | Line 159: | ||
|} | |} | ||
'''''Note'':''' If a match is not found the function returns a null value. This means the poor old browser needs to do a little more work to resolve the IP address. It checks the hosts file then any local DNS server, as a last resort puts a request onto the Internet to a DNS server. | '''''Note'':''' If a match is not found the function returns a null value. This means the poor old browser needs to do a little more work to resolve the IP address. It checks the hosts file, then any local DNS server, and as a last resort puts a request onto the Internet to a DNS server. | ||
'''''[[#top | Top]]''''' | '''''[[#top | Top]]''''' | ||
Line 169: | Line 169: | ||
*127.0.0.1 www.'''ric.com''' | *127.0.0.1 www.'''ric.com''' | ||
With a PAC file specific strings can be | With a PAC file, specific strings can be targeted. We are interested in resolving domain names and not what is typed into a browser. I have highlighted in bold these domain names. There are only two domains to resolve: '''mine.nu''' and '''ric.com''' hence our PAC file requires only two lines as follows: | ||
{| cellpadding="4" cellspacing="1" style="background:#000000;" | {| cellpadding="4" cellspacing="1" style="background:#000000;" | ||
Line 187: | Line 187: | ||
=== Alternative function === | === Alternative function === | ||
If you do not like using wild cards you can use the function dnsDomainIs to directly target a domain name. | If you do not like using wild cards, you can use the function dnsDomainIs to directly target a domain name. | ||
'''dnsDomainIs(host, domain)''' | '''dnsDomainIs(host, domain)''' | ||
Line 244: | Line 244: | ||
'''''[[#top | Top]]''''' | '''''[[#top | Top]]''''' | ||
== Feedback from forum (27-6-07) == | == Feedback from the forum (27-6-07) == | ||
This was posted by '''figment88''' and provides another interesting way to use a PAC file; you can read the full post here [http://forum.uniformserver.com/index.php?showtopic=1216 Uniform Forum]. All I have done is | This was posted by '''figment88''' and provides another interesting way to use a PAC file; you can read the full post here [http://forum.uniformserver.com/index.php?showtopic=1216 Uniform Forum]. All I have done is cut and paste to here. | ||
I wanted to add an extra tip on top of the UniCenter tutorial. The tutorial shows you how to setup a proxy file so you can have name based virtual hosts without modifying the current computer's HOST file. The examples in the tutorial require you to modify your proxy file (*.pac) to add a line for every new virtual host. In addition, the examples use wildcard matching, so if you name your local site "example" and the real site is "example.com" the browser might not let you load both. | I wanted to add an extra tip on top of the UniCenter tutorial. The tutorial shows you how to setup a proxy file so you can have name based virtual hosts without modifying the current computer's HOST file. The examples in the tutorial require you to modify your proxy file (*.pac) to add a line for every new virtual host. In addition, the examples use wildcard matching, so if you name your local site "example" and the real site is "example.com" the browser might not let you load both. | ||
Line 271: | Line 271: | ||
site2 is site2.com | site2 is site2.com | ||
---- | ---- | ||
'''''Comment'':''' My original | '''''Comment'':''' My original pac file contained wildcard matching, not intentional since I especially wanted to target each site and prevent my browser looking on the Internet. I have included both versions for completeness. In contrast figment88 wants to view his development site locally and the published version hence his neat solution. | ||
== Summary == | == Summary == | ||
The above was just a quick introduction to the PAC file; it is much more powerful and worth further investigation | The above was just a quick introduction to the PAC file; it is much more powerful and worth further investigation. Search the Internet for more information. I have only covered what we need for our purpose. The '''test.pac''' file is about as complex as it gets. | ||
On the next page I show how to [[Virtual Hosting: Browsers and PAC | plumb the PAC]] file into your browser. | On the next page I show how to [[Virtual Hosting: Browsers and PAC | plumb the PAC]] file into your browser. |