What is NGINX? and how to set it up on Mac.

VenuThomas
7 min readMar 12, 2023
NGINX

In this story, we are asked to implement load balancing on our web. What is Load Balancing? Load Balancing is a technique for allocating the traffic load on two or more connecting lines in a balanced way. Example: Companies use a server to manage more than one client or user. When the number of users increases, the number of servers increases and a load balance is used to split the work of each server. It means, When a network is used by thousands of users, a server will be unable to serve all with optimal capabilities. The more people accessing it makes it work harder, so it gets heavier and heavier over time.
Load balancers have certain algorithms to divide the work between one server and another. For example, if there is a service request while server A is busy, the work will be moved to server B or C. There are also those that divide the server one by one. Each time there is a request, the load balancer reroutes to server A, then B, and then C. All this is done for no server to have an excessive workload.

Load Balancer System Designs

Load Balancing has many benefits including being able to provide fast response times, can share the load on the server evenly, and also protect users from servers that are down. Many load balancers are available today such as NGINX, HAProxy, Zevenet and many others. In this story I use NGINX for my Load Balancer choice. Its an open source software web server and main alternative/competitor to Apache HTTP Server.

There are several steps that I did:

If you haven’t installed brew yet, use this to install brew first which acts as a package manager for Mac OS. Open the mac terminal and use the command:

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

For further information about brew, please visit brew.sh.
Use brew to install NGINX, you just have to run the following command in the terminal.

brew install nginx

Upon completion of installation, a new folder will be created on your system.

/usr/local/etc/nginx

This directory contains the nginx.conf file, that is the nginx configuration file. I will tell you later about it.

You can turn it on using the following command to start the NGINX server

sudo nginx

To run a test, enter the URL in your browser and try this:
http://localhost:8080

Next is change the default port. The default port for NGINX is 8080, we will change it to 80. First stop the NGINX server if you run this in terminal:

sudo nginx -s stop

Then, open nginx.conf with any editor like TextEdit application

And change the server:

server {
listen 8080;
server_name localhost;

#charset koi8-r;

#access_log logs/host.access.log main;

location / {
root html;
index index.html index.htm;
}

to

server {
listen 80;
server_name localhost;

#charset koi8-r;

#access_log logs/host.access.log main;

location / {
root html;
index index.html index.htm;
}

Save the settings and start NGINX server by

sudo nginx

To run a test, enter the URL in your browser and try this:
http://localhost OR http://localhost:80

If you want your own customized page, instead of the default nginx, you can go to the directory:

/usr/local/var/www

Here you can edit the index.html file according to your needs. This folder may contain CSS or images that you can link to your HTML. And now we are ready to develop! Our web server is already set up.

Alternatively, we’re going to create two index.html files in different directories with different messages, like the following code:

Save both files and remember both paths

Stop the NGINX server if you are already running:

sudo nginx -s stop

Create a new _hello.conf configuration file in the “servers” directory in the NGINX configuration directory by using the command below. For the name of the new configuration file, you can choose any name, such as example.conf or sample.conf

nano /usr/local/etc/nginx/servers/_hello.conf

Write the following code inside it:

I put two values of both “root” parameters in the configuration file that is the path for the two index.html files I created earlier. Also, I put different ports “listen” for each directory of the index.html file. Save config file and start NGINX server by

sudo nginx

To run a test, enter both URLs in your browser:
http://localhost:9000/ AND http://localhost:9001/

Yes! Our web server is working fine!

Setting NGINX as a reverse proxy

You can configure the NGINX web server to act as a reverse proxy for HTTP traffic. A reverse proxy is a service that accepts a request from a client, sends the request to one or more proxy servers, retrieves the response, and returns the server’s response to the client. Due to its performance and scalability, NGINX is often used as a reverse proxy for HTTP and non-HTTP servers.

Open the _hello.conf file by below command to edit it.

nano /usr/local/etc/nginx/servers/_hello.conf

And add the following configuration to the server block that should provide the reverse proxy:

server {
listen 9002;
server_name localhost;

#charset koi8-r;

#access_log logs/host.access.log main;

location / {
proxy_pass http://localhost:9001/;
}

}

The modified _hello.conf file looks like this:

The proxy server URL is set via proxy_pass, which can use HTTP or HTTPS as the protocol, domain name or IP address, an optional port as the address, and URI. And, start NGINX server by

sudo nginx

If a visitor logs in at http://localhost:9002, Nginx will send this request to http://localhost:9001.

NGINX reverse proxy configuration with Load Balancing.

You must configure a load balancing server that distributes the load between the two application servers.

Open the _hello.conf file by below command to edit it.

nano /usr/local/etc/nginx/servers/_hello.conf

Place the following code in the _hello.conf file

upstream WebPool {
server localhost:9000;
server localhost:9001;
}

server {
listen 9003;
server_name localhost;

location / {
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_pass http://WebPool;
}
}

Save and close the file when you’re done and start NGINX server by

sudo nginx

The modified _hello.conf file looks like this:

To run a test, open your web browser and access the load balancing server using the URL http://localhost:9003. You will be redirected to the 1st application server (localhost:9000) as shown below:

Then refresh the page continuously after a specified time and your application will be loaded from the 2nd application server (localhost:9001) as shown below:

To use the least-connected strategy, you must specify the least_conn directive in the upstream directive.

upstream WebPool {
least_conn;
server localhost:9000;
server localhost:9001;
}

The ip_hash configuration applies to servers defined to make the load balancer adherent, meaning that requests are distributed among servers based on clients’ IPv4 address or full IPv6 address.

upstream WebPool {
ip_hash;
server localhost:9000;
server localhost:9001;
}

If there is a server that we want to process more requests because it has more capacity, we can give more weight to it. In this configuration of every 4 HTTP requests, 3 requests will go to localhost:9000 and one request to localhost:9001.

upstream WebPool {
server localhost:9000 weight=3;
server localhost:9001;
}

In the example code below, after 3 failures on the 1st application server (localhost:9000), it will be considered unavailable for 60 seconds.

upstream WebPool {
server localhost:9000 max_fails=3 fail_timeout=60s;
server localhost:9001;
}

See http://nginx.org/en/docs/http/ngx_http_upstream_module.html for more configurations in the upstream directive.

And some more information about NGINX below:

Main locations:

  • NGINX has been installed in: /usr/local/Cellar/nginx
  • Default config file is: /usr/local/etc/nginx/nginx.conf
  • Add configs in (NGINX will load all files): /usr/local/etc/nginx/servers/
  • Logs will be in: /usr/local/var/log/nginx/
  • Document root is: /usr/local/var/www/
  • Default listen address is: http://localhost:8080

NGINX Commands:

  • Start NGINX server: sudo nginx
  • Stop NGINX server : sudo nginx -s stop
  • Restart NGINX server: sudo nginx -s reload
  • NGINX Help: nginx -help

More details will be posted. Thank you for reading this article! ❤️

If you would like to ask me questions, please feel free! I’ll always have my mailbox open. Whether you have a question or would like to simply say hello. I will do my utmost to respond!

--

--