Nginx adalah web server open-source yang ringan, cepat, dan banyak digunakan untuk melayani HTTP, reverse proxy, load balancing, dan caching.
Dokumen ini membahas instalasi, konfigurasi dasar, best practice, security hardening, serta setup SSL dengan Let's Encrypt.
sudo apt update
sudo apt install nginx -y
sudo yum install epel-release -y
sudo yum install nginx -y
sudo systemctl enable nginx
sudo systemctl start nginx
systemctl status nginx
File konfigurasi utama:
/etc/nginx/nginx.conf
/etc/nginx/sites-available/
/etc/nginx/sites-enabled/
(Debian/Ubuntu style)Contoh konfigurasi virtual host sederhana:
server {
listen 80;
server_name example.com www.example.com;
root /var/www/example.com/html;
index index.html index.htm;
access_log /var/log/nginx/example_access.log;
error_log /var/log/nginx/example_error.log;
location / {
try_files $uri $uri/ =404;
}
}
Aktifkan konfigurasi:
sudo ln -s /etc/nginx/sites-available/example.com /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx
Nonaktifkan versi Nginx di header
server_tokens off;
Batasi ukuran upload
client_max_body_size 10M;
Proteksi dari klikjacking & XSS
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options nosniff;
add_header X-XSS-Protection "1; mode=block";
Batasi metode HTTP
if ($request_method !~ ^(GET|POST|HEAD)$ ) {
return 444;
}
Rate Limiting (DoS protection)
limit_req_zone $binary_remote_addr zone=mylimit:10m rate=10r/s;
server { location / { limit_req zone=mylimit burst=20 nodelay; } }
6. **Gunakan user non-root untuk worker process**
Pastikan di `/etc/nginx/nginx.conf`:
```nginx
user www-data;
Debian/Ubuntu:
sudo apt install certbot python3-certbot-nginx -y
RHEL/CentOS:
sudo yum install certbot python3-certbot-nginx -y
sudo certbot --nginx -d example.com -d www.example.com
Certbot akan otomatis mengedit konfigurasi Nginx dan menambahkan SSL.
Cek konfigurasi:
sudo nginx -t
sudo systemctl reload nginx
Certbot membuat cron job otomatis, bisa diuji dengan:
sudo certbot renew --dry-run
✅ Gunakan server_tokens off
untuk menyembunyikan versi Nginx.
✅ Pisahkan konfigurasi setiap domain di sites-available
.
✅ Gunakan access_log
dan error_log
untuk monitoring.
✅ Batasi ukuran upload (client_max_body_size
).
✅ Gunakan firewall (UFW, iptables, atau security group) untuk hanya membuka port 80 & 443.
✅ Aktifkan rate limiting untuk mengurangi brute-force/DoS.
✅ Gunakan SSL (Let's Encrypt) dengan auto-renew.
✅ Selalu update Nginx ke versi terbaru untuk keamanan.
✅ Lakukan benchmark & tuning parameter worker (worker_connections
, worker_processes
) sesuai resource server.