Engineer bro!

Sat Mar 19 2022 (2 years ago)

Engineer by mistake!

I am a software engineer by passion

Using Nginx as a reverse proxy

Nginx, stylized as NGIИX, is a web server that can also be used as a reverse proxy, load balancer, mail proxy and HTTP cache. In this article, we'll see the basics of Nginx installation and setting up the proxy.

Prerequisite

This article requires you to have some basic understanding of Nginx, Proxy, Reverse Proxy, etc.

What is Nginx?

NGINX is open-source software for web serving, reverse proxying, caching, load balancing, media streaming, and more. It started out as a web server designed for maximum performance and stability. In addition to its HTTP server capabilities, NGINX can also function as a proxy server for email (IMAP, POP3, and SMTP) and a reverse proxy and load balancer for HTTP, TCP, and UDP servers.

What is a proxy?

A proxy server acts as a gateway between you and the internet. It's an intermediary server separating end users from the websites they browse. Proxy servers provide varying levels of functionality, security, and privacy depending on your use case, needs, or company policy.

Types of proxy

There are two types of proxy.

  1. Forward Proxy - A forward proxy, or gateway, or just "proxy" provides proxy services to a client or a group of clients. There are likely hundreds of thousands of open forward proxies on the Internet. They store and forward Internet services (like the DNS, or web pages) to reduce and control the bandwidth used by the group.
    Forward proxies can also be anonymous proxies and allow users to hide their IP addresses while browsing the Web or using other Internet services. TOR (The Onion Router), routes internet traffic through multiple proxies for anonymity.

  2. Reverse Proxy - As the name implies, a reverse proxy does the opposite of what a forward proxy does: A forward proxy acts on behalf of clients (or requesting hosts). Forward proxies can hide the identities of clients whereas reverse proxies can hide the identities of servers. Reverse proxies have several use cases, a few are:

    1. Load balancing: distribute the load to several web servers.

    2. Cache static content: offload the web servers by caching static content like pictures.

    3. Compression: compress and optimize content to speed up load time.

Installing Nginx

This tutorial has been tested on Ubuntu 20.04

Step 1

sudo apt update
sudo apt install nginx

Step 2 – Adjusting the Firewall

sudo ufw app list

Step 3 – Checking your Web Server

systemctl status nginx

See this document for more information - https://github.com/ats1999/to-learn/tree/main/nginx

Now, you have installed Linux then we can start creating proxies.

Creating Proxies

To create the proxy, we need to edit Nginx configuration file. You can see the content of Nginx configuration file by using /etc/nginx/sites-enabled/default.

In order to make changes to this file, we can get it into current working directory.

cp /etc/nginx/sites-enabled/default .

Add the below content for proxy.

location /users/ {
    proxy_pass http://localhost:4001/;
}

location /products/ {
    proxy_pass http://localhost:4000/;
}

If the user will navigate to /users/x/y/z... then the request will be proxied to http://localhost:4001/x/y/z....

If the user will navigate to /products/x/y/z... then the request will be proxied to http://localhost:4000/x/y/z....

Currently, we have only edited config into current working directory, we need to place it back to /etc/nginx/sites-enabled.

sudo cp default /etc/nginx/sites-enabled/

Once there is a change in the configuration, we need to reload Nginx.

sudo systemctl reload nginx

Now our proxy will start working :)

Software EngineeringNodeJsNGINX

© 2021 dsabyte. All rights reserved