Wildcard host resolution for Windows/macOS

/
7

In a previous post, I detailed an Acrylic DNS setup for Windows in which you can have a regular expression match for host resolution. This is great for local testing, and vhosts (virtual hosts), but there was no simple way to replicate this setup on macOS.

I've known about the Automatic Proxy Configuration ability for a while but never really had a use for it, until now! This new solution works in both Windows and macOS, and probably Linux -- for all modern browsers that take advantage of automatic proxy configuration scripts.

For example, I have a folder, /Volumes/I/_WORK/, where I put my projects. I had setup Acrylic and my vhosts config so that I could access the project folders dynamically via my Apache server; *.stage adjusts the document root to the /Volumes/I/_WORK/*/www folder automatically, and I don't have to add a line in my hosts file every time.

Examples:

    tests.stage		=>	/Volumes/I/_WORK/tests/www/
    www2.tests.stage	=>	/Volumes/I/_WORK/tests/www2/
    project1.random.stage	=>	/Volumes/I/_WORK/random/project1/

We're going to set up resolution of *.stage to 127.0.0.1 with an Automatic Proxy Configuration script.

Without any further delay, to match *.stage -- test.stage, websitename.stage etc -- you can use the following setup:

For macOS

  1. Create ~/.proxy.pac...
 function FindProxyForURL(url, host) {
     if (shExpMatch(host,"*.stage")) {
         return "PROXY 127.0.0.1";
     }
     return "DIRECT";
 }
  1. Setup ~/.proxy.pac in your network preferences:
  1. Open Network under System Preferences
  2. Select your active network connection and click "Advanced..."
  3. Open the "Proxies" tab
  4. Check "Automatic Proxy Configuration" under "Select a protocol to configure"
  5. For the Proxy Configuration file URL, enter the following -- replacing USERNAME with your actual username: file:///Users/USERNAME/.proxy.pac
  6. Hit OK, and Apply.

For Windows

  1. Create proxy.pac (same as above)...
  2. Setup proxy.pac in your network preferences:
  1. Open "Internet Options" under Control Panel
  2. Click the "Connections" tab, and click "LAN Settings"
  3. Check "Use automatic configuration script" under "Automatic Configuration"
  4. For "Address" enter the path to your proxy.pac, ex: file:///C:/Dropbox/proxy.pac
  5. Hit OK, and Apply.

If you have setup your regular expression virtual hosts for Apache or nginx already, you should now be able to access your local webserver using *.stage hostnames.

If you need an example for nginx virtual hosts, here is my relative nginx config block:

server {
    listen   127.0.0.1:80;
    index  index.html index.htm index.php;
    # regex magic to detect AAA.BBB.stage or AAA.stage hostnames,
    server_name   ~^((?<subdomain>[^\.]+)\.(?<domain2>.+)\.stage|(?<domain>.+)\.stage)$;
    # my projects stay in I:/_WORK/AAA/ and have www folders or subfolders (I:/_WORK/AAA/BBB)
    set $new_root I:/_WORK/$domain/www/;
    if ($subdomain != '') { set $new_root I:/_WORK/$domain2/$subdomain/; }
    if (!-e $new_root$document_uri){return 404;}
    log_not_found off;
    charset utf-8;
    access_log  logs/access.log  main;

    location ~ /\. {
        deny all;
    }

    location ~ \.php$ {
        root   $new_root;
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME	$document_root$fastcgi_script_name;
        include        fastcgi_params;
        fastcgi_param  SERVER_NAME	$http_host;
    }

    location / {
        root  $new_root;
    }
}

Now my browsers hit the proxy script, see *.stage as 127.0.0.1, and hit my local nginx setup which recognizes the * .stage vhosts and routes the request to the correct project directory.

No more manually adding entries to the hosts file! (for website dev, anyways)

Update 12/28/2012: Added fastcgi_param for SERVER_NAME

Disclaimer: this is probably not a great configuration for production performance, so modify and test accordingly if you are going to use the nginx config w/ RegEx in a production environment.


TOY MODE
π