(Originally published by MC Press Online in December 2015).

In the article “Drivers, Start Your Nginx!”, I discussed how to install the Nginx web server and walked through how to setup a simple index.html hello world program.  An obvious next step would be to have Nginx act as a front-end to your application servers so it can accomplish what’s called a “reverse proxy”.

Wikipedia defines a reverse proxy as:

… a type of proxy server that retrieves resources on behalf of a client from one or more servers. These resources are then returned to the client as though they originated from the proxy server itself.”

In our scenario we will be using Nginx to front a number of Node.js servers for load balancing purposes.  Below I’ve created a simple Node.js application that can have the port specified when it is started.  The variable port is necessary because we will have each Node.js application listening on separate ports and have Nginx round-robin to each one to spread the work around.

— app.js —

Note that specifying 127.0.0.1 as the IP address means the Node.js app will only accept local requests.  This inherently means I won’t be able to access the Node.js application from my laptop’s browser by specifying http://myibmi:5001 and instead will only allow connection attempts that happen from within my IBM i.  This is good because I don’t want the Node.js app to be accessible directly and instead want all traffic to go through Nginx.

Below is the Nginx configuration that acts as a proxy for the Node.js application servers.

— nginx.conf —

The proxy_pass directive declares the protocol (http) and address of our Node.js application server.  There are some scenarios you only need to proxy a single application server but in our scenario we have multiples.  That’s where the Nginx upstream feature comes into play as a mechanism to specify a group of servers.  Note how the proxy_pass is connected to upstream by the “node_servers” name.

Now start the Nginx server, as shown below.  I included pwd and cd commands to give context.

At this point I haven’t actually started my Node.js application servers.  If I visit the URL where Nginx is listening I will get a 502 Bad Gateway error, as shown below.

f1

This error can be modified to convey that the site it under construction because it many times means the application servers are being restarted because of fresh deployment.

Now we’ll go ahead and start both Node.js servers in separate shell sessions, as shown below.

You’ll see the console.log statement output is in play and is letting us see a timestamp of when each request is processed.  If you look closely you can see the timestamps alternate between the two shell sessions giving us confidence that Nginx is in fact doing a round-robin between the two servers.  Victory!

This concludes the Nginx reverse proxy tutorial.

Now, I have a question for you.  

What are some other things you want to know concerning open source on IBM i?

I generally write from three foundations:

1) Incrementally build a storyline of what I know you need to know.

2) Choose topics from real-world customer examples.  

3) Get feedback from readers on topics they’d like to know more about.  

So, what other topics would you like to see covered?  Is further detail needed to clarify previous topics of personal interest?

Let me know in the comments or share with me directly at abartell@krengeltech.com.

 

Get Social

Share to your favorite social platform