Skip to main content

Build Up A Webserver With Nginx On IBM i

In this post, IBMer Ji Mo walks through the steps of building a nginx server on IBM i.

It’s been nearly two years since we first released the open-source nginx server for IBM i. It has been a great addition to our open-source toolset for a couple of reasons. First off, nginx is lightweight, flexible and performs well. Plus, finding help is easy, since it provides usage that is consistent with what you’d see on other platforms, such as Linux. Today we will learn how to get started, thanks to this how-to article written by IBMer Ji Mo. You can be deploying web applications or APIs quickly and easily!

Assuming there’s a simple demand of deployment for your business, you need to set up Nginx server to serve the static-content files. You’ll also want the Nginx server to pass dynamic-content requests to Node.js servers behind walls.

First of all, let’s acquire Nginx and Nodejs packages. You can download them from the IBM i website or build the installation packages from source code in IBM i open-source support repositories on GitHub. Here are package versions installed that I’m playing with:

bash-4.4$ nginx -v
nginx version: nginx/1.15.2
bash-4.4$ node -v
v8.15.1

Figure 1

Now we start with the configuration file of Nginx. The nginx.conf file sits in directory ‘/QOpenSys/etc/nginx/’. Let’s make changes to meet our simple demand as Figure 2.

http {
...
server {
# Specify your favorite port
listen 80;
# Specify your host name
server_name ut50p12;
...
location / {
# Specify your own root location for static files
root /QOpenSys/pkgs/share/nginx/html;
...
}
... 

Figure 2

Perhaps you might create directory ‘/QOpenSys/pkgs/logs/’ to hold nginx.pid for Nginx if it weren’t there.

bash-4.4$ mkdir -p /QOpenSys/pkgs/logs

Figure 3

Isn’t it fantastic that no more actions are needed? We can start Nginx up now! You can add the path of nginx into your environment variable if you are not a big fan of typing.

bash-4.4$ /QOpenSys/pkgs/bin/nginx​

Figure 4

Now you can open your favorite web browser and input URL ‘http://ut50p12’. If you get the default welcome page of Nginx like my setup or the contents from your own root directory, congratulations! Your first Nginx server on IBM i is born!
 

In the next step, we are going to let Nginx work with Nodejs servers. Let’s prepare a piece of node.js code and name the script as ‘server.js’ to serve requests routed from the Nginx server.

​let http=require("http");

http.createServer((req, res) => {
    res.write('Welcome to nodejs!');
    res.end();
}).listen(8000, 'localhost');​

Figure 6

Then start the Node.js server up as a background process. This server will serve on port 8000 behind walls.

bash-4.4$ node server.js &

Figure 7

Let’s add a few more rules in nginx.conf.

http {
...
# Define a upstream server
upstream nodesvr {

server localhost:8000;
}
...
server {
# Specify your favorite port
listen 80;
# Specify your host name
server_name ut50p12;
...
location / {
# Specify your own root location for static files
root /QOpenSys/pkgs/share/nginx/html;
...
}
# route /mynode request to Nodejs server
location /mynode {
proxy_pass http://nodesvr;
}
...

Figure 8

The final step is to restart the Nginx server to put our changes into effect.

bash-4.4$ nginx -s reload

Figure 9

​Now we test if the Nginx works well with the Nodejs server. Type ‘http://ut50p12/mynode’ and hit Enter. You’re all set!

Welcome-to-node-js.jpg
Figure 10

If you want dig deeper with Nginx on IBM i, such as configuring SSL, compress, proxy balancing, etc., you can follow instructions in the Nginx official documents to tune your server.


Key Enterprises LLC is committed to ensuring digital accessibility for techchannel.com for people with disabilities. We are continually improving the user experience for everyone, and applying the relevant accessibility standards.