Master Nginx: From Basics to Advanced Configuration and Load Balancing
This guide walks you through Nginx fundamentals—including its role as a web server, proxy, reverse proxy, and load balancer—followed by step‑by‑step installation, configuration examples, and essential commands for managing a production‑grade Nginx setup.
Nginx is a versatile web server that can act as a reverse proxy, load balancer, mail proxy, and HTTP cache, following a master‑slave architecture.
In simple terms, a web server is an intermediary: a client requests https://dev.to, the browser contacts the web server, which forwards the request to a backend server and returns the response.
Proxy and Reverse Proxy
Proxy : The client sends a request to a proxy server, which forwards it to the backend server; the backend server does not know which client originated the request.
Reverse Proxy
A reverse proxy works the opposite way: a client contacts the proxy (web server), which then selects one of multiple backend servers to handle the request.
Load Balancing
Load balancing is a specific use of reverse proxy that requires two or more backend servers; it distributes incoming requests across them using algorithms such as round‑robin.
Stateful vs Stateless Applications
Stateful application : Each backend server stores its own instance‑specific data, which can lead to inconsistent results when different clients are routed to different servers.
Stateless application : Clients receive a token from the web server; the token is sent with each request, allowing any backend server to produce the same output.
What Is Nginx?
Nginx acts as a middleman between clients and backend servers. In a typical setup, three backend servers run on ports 3001, 3002, and 3003, sharing a common database on port 5432.
Installation Process
On macOS you can install Nginx with Homebrew: $ brew install nginx After installation, start Nginx and verify it is running: $ nginx Open http://localhost:8080/ in a browser to see the default page.
Basic Configuration Settings and Example
Create the following directory structure:
.
├── nginx-demo
│ ├── content
│ │ ├── first.txt
│ │ ├── index.html
│ │ └── index.md
│ └── main
│ └── index.html
└── temp-nginx
└── outsider
└── index.htmlConfigure Nginx to serve the /content and /outsider URLs, block .md files, and set up a reverse‑proxy on port 8888:
http {
server {
listen 5000;
root /path/to/nginx-demo/main/;
location /content {
root /path/to/nginx-demo/;
}
location /outsider {
root /path/temp-nginx/;
}
location ~ .md {
return 403;
}
}
server {
listen 8888;
location / {
proxy_pass http://localhost:5000/;
}
location /new {
proxy_pass http://localhost:5000/outsider/;
}
}
events {}
}Run the configuration with:
sudo nginxAdditional Nginx Commands
Test configuration syntax: sudo nginx -T Reload a running server: nginx -s reload Stop the server: nginx -s stop Check running processes and terminate if needed:
ps -ef | grep nginx
kill -9 <PID>Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
21CTO
21CTO (21CTO.com) offers developers community, training, and services, making it your go‑to learning and service platform.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
