How to debug OAuth2 locally

Jun 18, 2014

The main problem when debugging web applications that use OAuth2 is that the other end (Google+, Facebook, Twitter etc) allows you to access their authorization services only from sertain production domains (which usually do not include localhost). Fast and dirty edits at the production server are not a good idea. And it is not as convinient as editing your files locally.

I have come to the following schema: I have an nginx running locally and serving my local pages for my debugging purposes and I use the following trick:

Modify your /etc/hosts file

127.0.0.1   zmij.github.io # Name of your production server

Nginx configuration:

server {
    server_name zmij.github.io; # Name of your production server
                                # If you have a single web server locally,
                                # this name doesn't matter.
    listen 80;

    #**
    # Lot of cool settings
    #**

    # Proxy ordinary traffic to your production server
    location / {
        # Your production server most likely will serve
        # a number of sites, so set the Host header
        # not to confuse it.
        proxy_set_header    Host $http_host;
        # You cannot use the hostname here as it is your computer
        # now. Find out the ip address beforehand.
        proxy_pass      http://23.235.43.133:80;
    }

    # And here comes our debug location.
    # You should select the name for location that
    # will not clash with locations on the production server.
    location /local {
        # I proxy the traffic to jekyll watching my
        # pages changes. But you can set a document_root
        # as well.
        proxy_pass      http://localhost:4000/;
    }
}