Coding Fun – NGINX reverse proxy

YANRPA – Yet Another Nginx Reverse Proxy Article.  Seriously, there are a lot of good resources for reverse proxy.  I’m simply writing this as a documentation trail for what I did, so 6 months from now I’ll remember.

NGINX installation and setup will not be covered here, only the changes I’ve made to get the system to send data to my back-end server hosting the NodeJS script for the Imgur Spark Bot.

First, all of my config is done within nginx.conf, so go edit that with your editor of choice (VIM FTW!).  In my case, I’ve chosen to serve it up with a URI of /spark, seems simple and logical.  I won’t go into the intricacies of reverse proxy, just know that some back-end applications need to understand if their URI is changing.  If so they generally have a way to tell the application that.  Why is this important?  Because any links or paths need to be adjusted accordingly.  Remember that the reverse-proxy is simply acting like a client to the back-end server on behalf of the person asking IT for content.  It has to know where to go and what links to use.

Can you strip the path off?  Sure (with rewrite) but honestly it’s much harder.  In our case I’m going to ensure NodeJS knows that there’s a URI path that matches NGINX, and we won’t have these issues.

Here is my relevant nginx.conf stanza.

location /spark {
  proxy_pass http://10.0.1.51:90;
  proxy_redirect off;
  proxy_set_header Host $host;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

Easy enough?  I’m telling the publicly exposed NGINX server to send any request for /spark to the server at 10.0.1.51 listening on port 90.  I’m adding some headers (in case I ever want to log where Spark requests come from, or more importantly to IP restrict them to known spark servers).

This is honestly all that’s required on the NGINX server, once it’s done we can proceed to finally creating some NodeJS code.

One thought on “Coding Fun – NGINX reverse proxy

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s