Bab 4: Dasar Konfigurasi Nginx – Panduan Lengkap dari Sintaks hingga Directive

Bab 4: Dasar Konfigurasi Nginx – Panduan Lengkap dari Sintaks hingga Directive

Mengkonfigurasi Nginx dengan benar adalah kunci agar server stabil, cepat, dan aman. Di bab ini, kita membahas semua yang perlu dipahami pemula maupun admin: struktur konfigurasi, blok utama, directive penting, lokasi file, contoh server block nyata, dan tips best practice.

1. Sintaks Dasar Nginx

Nginx menggunakan sintaks berbasis blok:

  • Blok: {}
  • Directive: diakhiri ;
  • Komentar: #
  • Include file: memungkinkan konfigurasi modular

Contoh minimal:

worker_processes auto;

events {
    worker_connections 1024;
}

http {
    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    server {
        listen 80;
        server_name example.com;

        root /var/www/html/example;
        index index.html;

        location / {
            try_files $uri $uri/ =404;
        }
    }
}

Analisis:

  • worker_processes auto → jumlah worker menyesuaikan CPU
  • events → mengatur jumlah koneksi yang bisa ditangani per worker
  • http → blok utama HTTP server
  • server → virtual host per domain
  • location → routing path spesifik

2. Blok Utama dan Fungsinya

a. Main / Global Block

Terletak di awal file nginx.conf
Fungsi: pengaturan global server

user www-data;
worker_processes auto;
pid /run/nginx.pid;
  • user → user Linux yang menjalankan Nginx
  • worker_processes → jumlah worker proses
  • pid → lokasi file PID Nginx

Tips: Gunakan auto untuk worker_processes agar performa optimal sesuai jumlah core CPU.


b. Events Block

Mengatur koneksi dan worker process

events {
    worker_connections 1024;
    multi_accept on;
}
  • worker_connections → jumlah maksimal koneksi per worker
  • multi_accept → worker menerima banyak koneksi sekaligus

Best practice: Kombinasikan worker_connections x worker_processes untuk menghitung kapasitas maksimum koneksi simultan.


c. HTTP Block

Blok utama untuk HTTP server. Bisa memuat server block, caching, proxy, dan logging.

http {
    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    access_log /var/log/nginx/access.log main;
    error_log /var/log/nginx/error.log warn;

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

Catatan:

  • include → memuat file konfigurasi tambahan
  • access_log → mencatat request user
  • error_log → mencatat error server

d. Server Block

Blok ini mewakili virtual host/domain

server {
    listen 80;
    server_name example.com www.example.com;

    root /var/www/html/example;
    index index.html index.htm;

    location / {
        try_files $uri $uri/ =404;
    }

    location /api/ {
        proxy_pass http://127.0.0.1:3000;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}

Penjelasan:

  • listen → port untuk domain
  • server_name → nama domain yang dilayani
  • root → folder root website
  • index → file default ketika akses direktori
  • location /api/ → contoh reverse proxy ke backend

Best practice: Selalu set header di proxy_pass agar backend tahu request asli.


e. Location Block

Blok path spesifik dalam server block:

location /images/ {
    root /var/www/html;
}

location ~* \.(jpg|png|gif)$ {
    expires 30d;
}
  • Prefix match: /images/
  • Regex match: ~* \.(jpg|png|gif)$ → semua gambar
  • Bisa diatur caching, proxy, atau redirect

3. Directive Penting Nginx

DirectiveFungsiLokasi
listenPort serverserver block
server_nameNama domainserver block
rootFolder websiteserver/location
indexFile defaultserver block
try_filesFallback jika file tidak ditemukanlocation block
proxy_passProxy ke backendlocation block
access_logLokasi access loghttp/server block
error_logLokasi error loghttp/server block

Tips: Gunakan try_files $uri $uri/ /index.html; untuk SPA (Single Page Application).


4. Lokasi File & Direktori Nginx

/etc/nginx/
├── nginx.conf                  # konfigurasi utama
├── mime.types                   # tipe file
├── conf.d/                      # konfigurasi modular
│   └── default.conf
├── sites-available/             # semua virtual host tersedia
│   └── example.com
├── sites-enabled/               # virtual host aktif (symlink)
│   └── example.com -> ../sites-available/example.com
/var/www/html/                   # folder root website
├── index.html
/var/log/nginx/
├── access.log                   # log akses
└── error.log                    # log error

Tips:

  • Gunakan sites-available + sites-enabled agar mudah mengaktifkan/mematikan virtual host
  • Pisahkan konfigurasi tambahan di conf.d untuk modularitas

5. Reload vs Restart Nginx

  • Reload:
sudo nginx -t       # cek konfigurasi
sudo systemctl reload nginx

→ Memuat ulang konfigurasi tanpa memutus koneksi aktif

  • Restart:
sudo systemctl restart nginx

→ Menghentikan dan memulai ulang server, koneksi aktif akan terputus


6. Contoh Real-World Server Block

server {
    listen 80;
    server_name mysite.com www.mysite.com;

    root /var/www/mysite;
    index index.html index.htm index.php;

    # Static content caching
    location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
        expires 30d;
        add_header Cache-Control "public";
    }

    # Reverse proxy untuk backend API
    location /api/ {
        proxy_pass http://127.0.0.1:5000;
        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;
    }

    # PHP-FPM handling
    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php8.1-fpm.sock;
    }

    error_page 404 /404.html;
    location = /404.html {
        internal;
    }
}

Penjelasan:

  • Menangani static file, API proxy, PHP dalam satu server block
  • Menambahkan caching, error handling, headers untuk performa dan keamanan

Kesimpulan

Dasar konfigurasi Nginx mencakup:

  1. Sintaks blok dan directive (main, events, http, server, location)
  2. Directive umum dan penting (listen, server_name, root, index, proxy_pass)
  3. Lokasi file dan direktori untuk konfigurasi, virtual host, root website, dan log
  4. Cara reload vs restart Nginx
  5. Contoh konfigurasi real-world dengan static file, proxy, dan PHP-FPM

Dengan pemahaman ini, kamu siap membuat virtual host, reverse proxy, dan setup SSL di Bab selanjutnya.