- What Is Nginx and Why Use It?
Nginx (pronounced engine-x) is a high-performance web server and reverse proxy designed to handle large numbers of concurrent connections efficiently. It is commonly used to:
Serve static websites
Act as a reverse proxy for backend applications
Load balance traffic
Improve performance and security
Unlike traditional web servers, Nginx uses an event-driven, non-blocking architecture, which makes it fast, lightweight, and ideal for modern cloud and containerized environments.
- Common Nginx Deployment Architecture
For first-time deployments, Nginx is usually placed in front of your application:
User β Nginx β Application (Node.js / Python / PHP / Java)
Nginx handles:
Incoming HTTP/HTTPS requests
SSL termination
Static files
Forwarding requests to the app
Your application focuses only on business logic.
- Installing Nginx
On Ubuntu / Debian
sudo apt update
sudo apt install nginx -y
On CentOS / RHEL
sudo yum install nginx -y
Verify Installation
nginx -v
Start and Enable Nginx
sudo systemctl start nginx
sudo systemctl enable nginx
Open your browser and visit:
http://your-server-ip
You should see the Nginx welcome page.
- Understanding Key Nginx Directories
| Path | Purpose |
|---|---|
/etc/nginx/nginx.conf | Main configuration file |
/etc/nginx/sites-available/ | Available site configs |
/etc/nginx/sites-enabled/ | Enabled site configs |
/var/www/ | Website root directory |
/var/log/nginx/ | Access & error logs |
- Deploying a Static Website (First App)
Step 1: Create App Directory
sudo mkdir -p /var/www/myapp
sudo chown -R $USER:$USER /var/www/myapp
Step 2: Add Sample HTML
nano /var/www/myapp/index.html
<h1>My First Nginx App</h1>
<p>Deployment successful!</p>
Step 3: Create Nginx Server Block
sudo nano /etc/nginx/sites-available/myapp
server {
listen 80;
server_name myapp.com;
root /var/www/myapp;
index index.html;
location / {
try_files $uri $uri/ =404;
}
}
Step 4: Enable the Site
sudo ln -s /etc/nginx/sites-available/myapp /etc/nginx/sites-enabled/
Test configuration:
sudo nginx -t
Reload Nginx:
sudo systemctl reload nginx
Your static app is now live π
- Deploying a Backend App (Node.js Example)
Assume your app runs on port 3000.
Step 1: Application Running
node app.js
Step 2: Configure Nginx as Reverse Proxy
sudo nano /etc/nginx/sites-available/nodeapp
server {
listen 80;
server_name api.myapp.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;
}
}
Enable and reload:
sudo ln -s /etc/nginx/sites-available/nodeapp /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
Now users access:
http://api.myapp.com
instead of port 3000.
- Enabling SSL (HTTPS) with Letβs Encrypt
Step 1: Install Certbot
sudo apt install certbot python3-certbot-nginx -y
Step 2: Get SSL Certificate
sudo certbot --nginx -d myapp.com
Certbot automatically:
Installs SSL certificates
Updates Nginx config
Enables HTTPS
- Performance Optimization (Beginner Essentials)
Enable Gzip Compression
gzip on;
gzip_types text/plain text/css application/json application/javascript;
Cache Static Files
location ~* \.(css|js|jpg|png|gif|ico)$ {
expires 30d;
access_log off;
}
- Basic Security Best Practices
Disable server version:
server_tokens off;
Limit request size:
client_max_body_size 10M;
Allow only required ports (Firewall):
sudo ufw allow 'Nginx Full'
- Common Beginner Mistakes
β Forgetting to reload Nginx
β Syntax errors without running nginx -t
β App not running but proxy configured
β DNS not pointing to server IP
- When to Use Nginx in Real Projects
Use Nginx when:
You deploy production apps
You need HTTPS
You want better SEO and performance
You run microservices or APIs
- Final Thoughts
Nginx is often the first production-grade tool developers learn when deploying applications. Once you understand static serving and reverse proxy basics, you can confidently deploy real-world applications with better performance, security, and scalability.
Mastering Nginx is a huge step toward DevOps, cloud, and backend engineering π