A webserver that isn’t a webserver

The machine that sits at GT’s root domain is one that’s under my control. But, of course, I have nothing to do with GT’s main website. So obviously I want to redirect people who type in the address in a browser without the “www” to the www location.

Unfortunately, installing Apache (or similar) on that machine isn’t an option. It is a Linux box, however, so we can make it run a script when someone connects to the www port.

In order to do something like this, you start by opening /etc/inetd.conf and adding this:

www stream tcp nowait nobody [script location]

That says

  • on port www (which gets the number 80 from /etc/services)
  • with a streaming socket
  • over a TCP connection (TCP is generally stream, UDP is dgram)
  • do not wait for the previous socket to close before running again (allow multiple simultaneous executions)
  • run as user “nobody” (the user doesn’t have to be a real user on the system as long as permissions are set correctly)
  • run [script] (which should be an absolute location)

If you look in /etc/services you see that www can come across TCP or UDP, so both should really be handled:

www dgram udp nowait nobody [script]

Make sure your machine is listening on port 80:

netstat -an | grep 80
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN

If not, you’ll need to open that port with iptables which you can do like this:

iptables -I INPUT -p tcp --dport 80 -j ACCEPT

And do that for UDP as well.

Now it’s just a matter of making the script. It doesn’t have to be complicated at all, and for me, since I just want to redirect to the www address, I simply echo the redirect headers as outlined in RFC 2616 Sections 6.1 and 10.3.2. Essentially they say that a 301 redirect only needs a Location header in addition to the Status line. I do this with a simple bash script:

#!/bin/bash -r
echo 'HTTP/1.1 301 Moved Permanently'
echo 'Location: http://www.gatech.edu/'
echo
exit

Each echo causes a newline, and headers are supposed to end with two newlines. That’s why there’s an empty echo.

To support older systems and methods of navigating the internet (like gopher) I did add a little HTML after that so that if the redirect isn’t followed automatically, a link to the correct location would be provided, but what I have listed here is the minimum required for a 301 redirect.

You can test with Telnet, Netcat (nc on Linux if it’s installed), or Httpfox or Firebug in Firefox.

I don’t know why you’d want to, but you could even provide an actual webpage. The bash script would instead be

#!/bin/bash -r
echo 'HTTP/1.1 200 OK'
echo 'Content-Type: text/html; charset=UTF-8'
echo
cat [some file with HTML in it]
exit

Of course, bash is just what I used. PERL, PHP, or whatever other scripting-capable language would also work.

Some browsers don’t seem as reliable with this as others. Firefox works every time, but Safari sporadically doesn’t follow the 301 for me. If you can install apache, using mod_rewrite to do a redirect would be far better.

You can leave a response, or trackback from your own site.

One Response to “A webserver that isn’t a webserver”

  1. Tim says:

    that explains your tweet.

Leave a Reply

Subscribe to RSS Feed Wanna be bored by my life?