Install Web Server - Nginx di Linux

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.


⚙️ Instalasi Nginx

Debian/Ubuntu

sudo apt update
sudo apt install nginx -y

RHEL/CentOS/Rocky

sudo yum install epel-release -y
sudo yum install nginx -y
sudo systemctl enable nginx
sudo systemctl start nginx

Cek Status

systemctl status nginx

🛠️ Konfigurasi Dasar

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

🔐 Best Security Configuration

  1. Nonaktifkan versi Nginx di header

    server_tokens off;
  2. Batasi ukuran upload

    client_max_body_size 10M;
  3. 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";
  4. Batasi metode HTTP

    if ($request_method !~ ^(GET|POST|HEAD)$ ) {
    return 444;
    }
  5. 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;

🔒 SSL & HTTPS Setup dengan Let's Encrypt

Instal Certbot

Debian/Ubuntu:

sudo apt install certbot python3-certbot-nginx -y

RHEL/CentOS:

sudo yum install certbot python3-certbot-nginx -y

Generate & Install SSL Certificate

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

Auto Renew SSL

Certbot membuat cron job otomatis, bisa diuji dengan:

sudo certbot renew --dry-run

🚀 Best Practices untuk Nginx di Production

✅ 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.


📌 Kesimpulan

  • Nginx adalah web server cepat & efisien, cocok untuk aplikasi web modern.
  • Konfigurasi security sangat penting agar server tidak mudah diserang.
  • SSL dengan Let's Encrypt memberikan enkripsi gratis dan mudah dengan auto-renew.
  • Best practice: gunakan konfigurasi modular, monitoring log, tuning worker, dan aktifkan firewall + SSL.