What is NGINX?
- NGINX (pronounced "engine-x") is a lightweight, high-performance web server.
- It also acts as a reverse proxy, load balancer, and HTTP cache.
- Designed for maximum performance and scalability using event-driven architecture.
Why Use NGINX?
- Handles thousands of simultaneous connections.
- Used by high-traffic websites (e.g., Netflix, Dropbox, WordPress.com).
- Efficient in serving static files, balancing loads, and acting as a reverse proxy.
sudo apt update
sudo apt install nginxsudo yum install epel-release
sudo yum install nginxsudo systemctl start nginx
sudo systemctl enable nginxnginx -v # Check version
sudo systemctl status nginx # Check status| Path | Description |
|---|---|
/etc/nginx/ |
Main configuration directory |
/etc/nginx/nginx.conf |
Main config file |
/etc/nginx/sites-available/ |
Available site configs |
/etc/nginx/sites-enabled/ |
Enabled sites (linked from available) |
/var/www/html/ |
Default web root directory |
/var/log/nginx/ |
Access and error logs |
| Command | Purpose |
|---|---|
sudo systemctl start nginx |
Start the server |
sudo systemctl stop nginx |
Stop the server |
sudo systemctl restart nginx |
Restart the server |
sudo nginx -t |
Test configuration for errors |
sudo nginx -s reload |
Reload configuration without downtime |
sudo mkdir -p /var/www/helloworld
sudo nano /var/www/helloworld/index.htmlAdd this HTML:
<h1>Hello World from NGINX!</h1>sudo nano /etc/nginx/sites-available/helloworldPaste:
server {
listen 80;
server_name localhost;
root /var/www/helloworld;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}sudo ln -s /etc/nginx/sites-available/helloworld /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginxTo serve static content like HTML, CSS, JS:
- Place files in your
rootdirectory (e.g./var/www/your-site) - Ensure your server block has:
root /var/www/your-site;
index index.html;Forwarding requests to a backend app (e.g., Node.js on port 3000):
server {
listen 80;
server_name yourdomain.com;
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}Step 1: Install Certbot
sudo apt install certbot python3-certbot-nginxStep 2: Generate SSL Certificates
sudo certbot --nginx -d yourdomain.com -d www.yourdomain.comStep 3: Auto-Renew SSL
sudo certbot renew --dry-runhttp {
upstream myapp {
server 127.0.0.1:3000;
server 127.0.0.1:3001;
}
server {
listen 80;
location / {
proxy_pass http://myapp;
}
}
}| Use Case | Description |
|---|---|
| Static File Hosting | Serve HTML, CSS, JS directly |
| Reverse Proxy | Forward requests to app servers |
| Load Balancing | Distribute traffic among multiple backend servers |
| SSL Termination | Enable HTTPS using free SSL certificates |
| Log Type | Path |
|---|---|
| Access Log | /var/log/nginx/access.log |
| Error Log | /var/log/nginx/error.log |
tail -f /var/log/nginx/access.log
journalctl -u nginx🎯 You're now equipped to:
- Create and enable multiple sites.
- Serve static and dynamic content.
- Secure your web app with HTTPS.
- Scale your app using load balancing.
- Rate limiting
- Gzip compression
- Caching static assets
- NGINX Plus (commercial version)
🚀 Congratulations! You’re now well on your way to becoming an NGINX Hero.