Image 01

Transneptune

beyond the Kuiper Belt, over the sea

Forward

October 21st, 2012 by Kit

So, John recently showed me this cool service, Foward. They are useful for two main things: showing off your development on localhost to people on the other side of the NAT, and developing on localhost with calls to external services that need to then call you back.

Basically, it’s just a reverse proxy.

So, without intending to steal their thunder (they really are very full-featured—take a look), here’s how to make a bare-bones version of this yourself.

What you’ll need:

  • A domain name. I’m using transneptune.net, of course.
  • A webserver, with proxying capability. I’m using Apache, with mod_proxy.
  • SSH keys (not strictly necessary, but it makes it all much more convenient).

So, there are two components to this: the reverse proxy on your webserver, and the script that makes an SSH tunnel from your computer to the webserver, which the proxy then forwards to.

Both are really super simple.

Here’s the Apache config:

<VirtualHost *:80>
ServerAdmin webmaster@localhost
ServerName forward.example.com

ProxyPass / http://localhost:8001/
ProxyPassReverse / http://localhost:8001/
</VirtualHost>

The port (8001) is arbitrary; I chose a high-numbered port that I was unlikely to use for anything else. The ServerName is also just an example.

Here’s the script to run locally to connect:

#!/bin/bash

PORT=$1
ssh -N -R 8001:localhost:$PORT example.com

The important part of this is the -R 8001:localhost:$PORT, which handles creation of the tunnel. The -N just keeps it from making a shell on the server when you run it.

So, an example of use:

Start your Django local development server:

$ ./manage.py runserver

This is running on port 8000.

Run the forwarding script, with the local port you’d like to make externally visible:

$ forward 8000

Wait a moment for it to connect.

Go to forward.example.com in a browser, or better yet, direct someone elsewhere to do so.

Celebrate.