Skip to content

Instantly share code, notes, and snippets.

@tuxcanfly
Created September 24, 2010 07:40
Show Gist options
  • Save tuxcanfly/595005 to your computer and use it in GitHub Desktop.
Save tuxcanfly/595005 to your computer and use it in GitHub Desktop.

Testing webhooks in development platforms

Webhooks were popularized by PayPal and have become commonplace now a days. A typical webhook service fires a POST to a URL of your choice whenever something interesting happens. You handle this request and respond accordingly.

But you need to test webhooks regularly during development and it is a bit of a problem if your development server is inaccessible from the rest of the Internet. I have tried to solve this using ssh and nginx.

For this method to work you need:

  • Development server (which can hopefully process the webhook at /webhook) lets call it (D)
  • SSH accessible server (S)
  • nginx to be installed on the (S)

We will ssh into the server with a 'backdoor' that will forward connections from (S) to (D). This is also called reverse port forward. On (D)

ssh -R 8888:localhost:80 user@(S)

See man ssh for details

Here we are telling ssh to create an incoming port (8888) on (S) and forward them to (D):80. So if you access localhost:8888 on (S), ssh will call (D):80 and return the response from (D). Note that you can only call localhost:8888 from (S) right now, because the port 8888 SSH creates does not listen on the public address. This means that calling (S):8888 from the internet will fail. Now, we need to forward webhook calls to localhost:8888 (on (S)). Yo Dawg!

Enter ngnix:

nginx is a reverse proxy that can listen to calls from public address and forward them to local ports. Now we can listen to the webhook on (S) and forward the request to localhost:8888. Here's how: On (S) location /webhook { proxy_set_header Host $http_host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass http://localhost:8888/webhook; }

this part lets nginx know that webhook requests should be forwarded to localhost:8888. What is localhost:8888? Oh yes, its the backdoor to (D):80 that SSH created.

The result:

  • Webhook provider calls (S)/webhook.
  • nginx receives /webhook and forwards to localhost:8888/webhook
  • ssh receives localhost:8888/webhook and forwards to (D):80/webhook
  • Inception!

tl;dr

http://public_server/webhook ==nginx=> http://localhost:8888/webhook =ssh==> http://dev_server:80/webhook

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment