Skip to main content

Command Palette

Search for a command to run...

Nginx Reverse Proxy

IMPLEMENTATION BY A PROJECT

Published
Nginx Reverse Proxy
H

Enthusiastic about DevOps tools like Docker, Kubernetes, Maven, Nagios, Chef, and Ansible and currently learning and gaining experience by doing some hands-on projects on these tools. Also, started learning about AWS and GCP (Cloud Computing Platforms).

Step 1: Clone the Repository

Step 2: Install Nginx

Step 3: Install Docker

Step 4: Configure your app on Nginx

Step 5: Reverse Proxy Enabling

Let’s start our project…..🚀

Step 1: Clone the Repository

git clone https://github.com/harshitsahu2311/Nginx-Reverse-Proxy-Project.git

Step 2: Install Nginx

Install Nginx server with the following command→

sudo apt update
sudo apt install nginx -y
sudo systemctl start nginx

Now check whether it is running or not on the <public-ip>.

Now go to /etc/nginx and you will find a file sites-enabledqa, go inside that directory

cd sites-enabled
vi default

Here you will see that the default port 80 is enabled and the traffic is routed from location/ path in the directory only.

  • You May also wonder that is what is a reverse proxy?

    A reverse proxy in Nginx is a server that sits before web servers and forwards client requests (like HTTP requests) to those web servers. It's called a reverse proxy because, instead of the client communicating directly with the backend server, the client interacts with the reverse proxy, which then forwards the request to the backend server.

    Why Use a Reverse Proxy?

    • Load balancing: Distribute traffic to multiple servers.

    • Security: Hide the details of the backend servers from the outside world.

    • Caching: Improve performance by caching responses from backend servers.

    • SSL Termination: Handle SSL encryption/decryption at the proxy, reducing the load on backend servers.

Step 3: Install Docker

Just copy all the commands below and run on the terminal

# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc

# Add the repository to Apt sources:
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
  $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update

sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
sudo usermod -aG docker $USER

Step 4: Configure your app on Nginx

Go to the App directory where app files is there,

cd /home/ubuntu/Nginx-Reverse-Proxy-Project

Run the Docker build command to build the image

docker build -t notes-app .

Run the Docker run command to execute the notes app file

docker run -d -p 8000:8000 --name django-note notes-app

Now your app is running on http://127.0.0.1:8000 you can check also by (127.0.0.1 = localhost)

curl -L http://127.0.0.1:8000

But if you want to access it you have to open port 8000 in your security group of EC2. But it is not a good practice to open a port because security breach problems arise from here only so we will redirect our app traffic through the nginx reverse proxy.

Step 5: Reverse Proxy Enabling

Go to configuration file of nginx

cd /etc/nginx/sites-enabled
vi default

Make some changes in the files, basically, we are adding a proxy in the location files so that it can interpret and route the traffic.

Now paste the public-ip in your browser, you will find a white screen is coming because you have not copy important accessible file of notes in /var/www/html

cd /home/ubuntu/Nginx-Reverse-Proxy-Project/mynotes/build
copy -r * /var/www/html
sudo systemctl restart nginx

Now Refresh the page and WOW you have successfully learn how to use reverse proxy feature NGINX server.

If you want to more these types of blogs in future, please let me know by the help of comment section.