|P is for Practical|
Viewing locally-served pages from the outside, while *inside*!by Phaysis (Pilgrim)
|on Sep 28, 2002 at 02:03 UTC||Need Help??|
Or, "My home webserver thought it was ugly until it looked in the mirror."
Over a year ago, I had a situation where I wanted to see what my locally-served website looked like from the "outside." This was in a drive to configure my home network and webserver to correctly serve pages to anyone I send my IP address, what with all the various configuration issues facing me at the time. My home network consists of an internal LAN fed by a Linksys cable-modem router with ports 80 and 8080 forwarded to my local webserver.
"But," you say, "why don't you just use the local internal IP of your webserver to see your site?"
"Well," I reply, "I want to make sure people on the outside are getting the same thing I'm getting from the inside with no surprises. Also, from the inside, if I use my router's external IP to view my site, I get the 401/403 login challenge on my router's web administration panel."
Hrumph indeed. So, everyone with me: "Perl to the rescue!"
What I needed was a script that resided on my externally-hosted and paid-for domain host that I could use to reflect an HTTP request. I thought of methods to make it call only my home's IP (all of them smelly, mangled, and snarled), but I settled on making it general enough to be used from any system that had a running webserver. Hey, what a neat idea!
Wanna try? If you have an externally-accessible server that answers to port 8080 on your externally-visible IP address (I had to cludge to circumvent my provider's "port 80 filter"...) then take a look at your site here. You will, in theory, get your locally-hosted site as seen from my hosted site. (Actual mileage may vary. Expect requests from phaysis.com in your access logs.)
On my hosted site, I have a subdirectory set aside in my document root which contains the mirror.cgi script and an .htaccess config file (your host must allow .htaccess files for this to work).
Further documentation assumes your mirror subdirectory is called /mirror .
The .htaccess file contents are as follows:
This tells Apache to serve mirror.cgi by default, which then executes to grab the root page on your local site. For any file on your local site which (surprise surprise) doesn't exist in the remote /mirror directory, Apache's ErrorDocument functions call your script to take care of file fetching.
Ok, so here's the meat and potatos of our operation. What we're doing is creating a LWP::UserAgent instance to get files from the IP of the computer making the request. Currently, the code kind of looks inelegant (my apologies) but it has worked very well for me in its 1 1/2 years of existence (further proving Aristotle's suggestion that "Makeshifts last the longest").
The code for mirror.cgi is as follows:
Hopefully, the code is simple and straightforward enough to warrant not much further explanation. It's a "straight wire" of a script, but it's just as effective. One caveat, though: this script doesn't handle POSTed form variables, only HTTP GET requests. I've considered extending to cover this, but currently there's little need.
So, now all I have to do to see my internal site as though I was outside is to, in my example, request the page http://www.phaysis.com/mirror/, and voila, my server's hard drive comes to life as it serves a page request for my root index file. As a bonus, and much to my amazement, the script even handles images with no problems thanks to the flexibility of LWP's methods.
I hope this is of use to some of you. It certainly has been useful to me in testing scripting and design of my local site (which is quicker and easier to update than my hosted space), as well as testing external access issues such as ISP's that filter what their paying customers can do, router port-forwarding issues, and local LAN connectivity issues. Very useful.
Heh, and just remember to ignore all the "404 /mirror/..." messages in your hosted domain's error_log files. Those'll get you.
-Shawn / (Ph) Phaysis