Nginx Installation & Config Guide
Nginx (engine x) is a high-performance HTTP and reverse proxy web server. It is known for its stability, rich feature set, simple configuration files, and low system resource consumption.
Web ServerNginx Installation
Linux (Ubuntu / Debian)
Install using apt package manager:
bash
sudo apt update
sudo apt install nginx -yCheck service status after installation:
bash
systemctl status nginxLinux (CentOS / RHEL)
Install using yum (you may need to install EPEL repository first):
bash
sudo yum install epel-release -y
sudo yum install nginx -yStart Nginx and enable on boot:
bash
sudo systemctl start nginx
sudo systemctl enable nginxWindows
- Go to Nginx Official Download Page and download the Stable version zip file.
- Unzip the downloaded file to any directory (e.g.,
C:\nginx). - Open Command Prompt (CMD) or PowerShell and navigate to that directory.
- Run
start nginxto start the service.
API Command Cheat Sheet
| Command | Description |
|---|---|
nginx -t | Test configuration file for syntax errors |
nginx -s reload | Reload configuration (without stopping service) |
nginx -s stop | Fast stop service |
nginx -s quit | Graceful stop (stop after processing current requests) |
Basic Configuration Examples
The main configuration file for Nginx is usually located at /etc/nginx/nginx.conf. It is recommended to create new .conf files in the /etc/nginx/conf.d/ directory to manage different sites.
1. Hosting a Static Website
Create a file named website.conf:
nginx
server {
listen 80;
server_name example.com; # Your domain or IP
root /var/www/html/my-website; # Website root directory
index index.html index.htm;
location / {
try_files $uri $uri/ =404;
}
}2. Reverse Proxy
Reverse proxies are commonly used to forward requests to backend application servers (like Node.js, Python, Go, etc.).
nginx
server {
listen 80;
server_name api.example.com;
location / {
proxy_pass http://127.0.0.1:3000; # Forward to local port 3000
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;
}
}