Search

NGINX adalah web server dan reverse proxy yang ringan, cepat, dan hemat resource, sering digunakan untuk load balancingserving static content, dan reverse proxying. Artikel ini akan membahas cara konfigurasi dasar hingga lanjutan NGINX.

1. Instalasi NGINX

Di Ubuntu/Debian

sudo apt update
sudo apt install nginx
sudo systemctl start nginx
sudo systemctl enable nginx  # Auto-start saat boot

Di CentOS/RHEL

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

Verifikasi:

sudo systemctl status nginx
curl http://localhost  # Cek apakah NGINX running

2. Struktur File Konfigurasi NGINX

NGINX menyimpan konfigurasi di /etc/nginx/:

  • nginx.conf → Konfigurasi utama.

  • sites-available/ → File konfigurasi virtual host (bisa dinonaktifkan).

  • sites-enabled/ → Symlink ke sites-available yang aktif.

  • conf.d/ → File konfigurasi tambahan.


3. Konfigurasi Dasar (Static Website)

Edit file di /etc/nginx/sites-available/default atau buat baru:

server {
    listen 80;  # Port HTTP
    server_name contoh.com www.contoh.com;  # Domain

    root /var/www/html;  # Direktori file website
    index index.html index.htm;  # File default

    location / {
        try_files $uri $uri/ =404;  # Cari file atau return 404
    }
}

Aktifkan Konfigurasi:

sudo ln -s /etc/nginx/sites-available/contoh.com /etc/nginx/sites-enabled/
sudo nginx -t  # Test konfigurasi
sudo systemctl restart nginx

4. Konfigurasi Reverse Proxy

NGINX bisa mengarahkan traffic ke backend (Node.js, Python, dll).
Contoh konfigurasi untuk aplikasi Node.js di port 3000:

server {
    listen 80;
    server_name api.contoh.com;

    location / {
        proxy_pass http://localhost:3000;  # Arahkan ke app Node.js
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

5. Load Balancing dengan NGINX

Contoh konfigurasi untuk distribusi traffic ke 2 backend server:

upstream backend_servers {
    server 192.168.1.10:8000;  # Backend 1
    server 192.168.1.11:8000;  # Backend 2
    server 192.168.1.12:8000 backup;  # Backup server
}

server {
    listen 80;
    server_name app.contoh.com;

    location / {
        proxy_pass http://backend_servers;
        proxy_set_header Host $host;
    }
}

Metode Load Balancing:

  • round-robin (default) → Bergantian.

  • least_conn → Ke server dengan koneksi paling sedikit.

  • ip_hash → Sesuai IP client (untuk session persistence).


6. HTTPS dengan SSL/TLS (Let’s Encrypt)

Gunakan Certbot untuk otomatisasi SSL:

sudo apt install certbot python3-certbot-nginx  # Ubuntu/Debian
sudo certbot --nginx -d contoh.com -d www.contoh.com

Konfigurasi NGINX akan otomatis terupdate dengan HTTPS.


7. Optimasi Performa NGINX

Caching Static Files

location ~* \.(jpg|css|js|png)$ {
    expires 30d;  # Browser cache 30 hari
    add_header Cache-Control "public";
}

Gzip Compression

gzip on;
gzip_types text/css application/javascript;
gzip_min_length 1000;

Rate Limiting (Anti DDoS)

limit_req_zone $binary_remote_addr zone=one:10m rate=10r/s;

location / {
    limit_req zone=one burst=20 nodelay;
}

8. Troubleshooting

  • nginx -t → Test konfigurasi sebelum restart.

  • tail -f /var/log/nginx/error.log → Cek error log.

  • sudo systemctl reload nginx → Restart tanpa downtime.


Kesimpulan

NGINX adalah web server & reverse proxy yang powerful dengan fitur:
✅ Static file serving
✅ Reverse proxy (Node.js, Python, dll)
✅ Load balancing
✅ HTTPS otomatis (Certbot)
✅ Optimasi caching & keamanan