How to setup NGINX for reverse proxy in Linux. 🐧

Muhammad Waleed Badar
2 min readDec 8, 2024

--

What is NGINX?

NGINX is a high-performance web server, commonly used as a reverse proxy and load balancer. It efficiently handles multiple connections with a process pool, optimizing resource utilization. NGINX also enables secure connections and supports various load-sharing mechanisms for scalable infrastructure.

Why use NGINX?

1. Single Entry Point

NGINX acts as a centralized gateway, routing traffic from a stable public IP address to various backend services (e.g., Home Assistant on port 8123) in a containerized or microservices architecture. This simplifies traffic management and ensures efficient load balancing.

2. Multiple Backend Apps

With NGINX, you can manage and route traffic to multiple backend services (e.g., different apps running on different ports), like the /ui/ service on port 3000, without any downtime. NGINX ensures smooth traffic flow even when scaling or updating services.

How to setup NIGINX.

Step 1: Install NGINX (if not already installed)

sudo apt-get install nginx

Step 2: Configure NGINX as a Reverse Proxy

Create a new NGINX configuration file for your reverse proxy. You can use the default configuration file located at /etc/nginx/sites-available/default or create a new one:

sudo nano /etc/nginx/sites-available/default

Inside the configuration file, add the following code to configure NGINX as a reverse proxy. Replace example.com with your domain or IP address and the ports accordingly:

server {
listen 8883 ssl;
server_name example.com;

# SSL certificates (Ensure these paths are correct)
ssl_certificate /etc/letsencrypt/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/example.com/privkey.pem;

# Reverse proxy for Home Assistant (Service 1) running on port 8123
location / {
proxy_pass http://127.0.0.1:8123; # Home Assistant backend

# Set headers
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Ssl on;
proxy_set_header X-Forwarded-Port 8883;
proxy_read_timeout 90;
proxy_connect_timeout 90;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}

# Reverse proxy for Grafana (Service 2) running on port 3000
location /ui/ {
proxy_pass http://127.0.0.1:3000; # Grafana backend

# Set headers
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Forwarded-Ssl on;
proxy_set_header X-Forwarded-Port 8883;
proxy_read_timeout 90;
proxy_connect_timeout 90;
}
}

Step 3: Reload NGINX to apply the changes:

sudo systemctl reload nginx

Conclusion:

In this tutorial, you learned how to set up a reverse proxy with NGINX, efficiently routing requests to backend servers. NGINX’s flexibility and performance make it an excellent choice for load balancing, SSL termination, and more.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Muhammad Waleed Badar
Muhammad Waleed Badar

Written by Muhammad Waleed Badar

I am passionate about Embedded Systems, IoT Development and Electronics HW Design. Exploring The Zephyr Project 🪁 while solving real-world problem using tech!⚡

No responses yet

Write a response