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.
Contents
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 CPUeventsβ mengatur jumlah koneksi yang bisa ditangani per workerhttpβ blok utama HTTP serverserverβ virtual host per domainlocationβ 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 Nginxworker_processesβ jumlah worker prosespidβ 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 workermulti_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 tambahanaccess_logβ mencatat request usererror_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 domainserver_nameβ nama domain yang dilayanirootβ folder root websiteindexβ file default ketika akses direktorilocation /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
| Directive | Fungsi | Lokasi |
|---|---|---|
listen | Port server | server block |
server_name | Nama domain | server block |
root | Folder website | server/location |
index | File default | server block |
try_files | Fallback jika file tidak ditemukan | location block |
proxy_pass | Proxy ke backend | location block |
access_log | Lokasi access log | http/server block |
error_log | Lokasi error log | http/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-enabledagar mudah mengaktifkan/mematikan virtual host - Pisahkan konfigurasi tambahan di
conf.duntuk 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:
- Sintaks blok dan directive (
main,events,http,server,location) - Directive umum dan penting (
listen,server_name,root,index,proxy_pass) - Lokasi file dan direktori untuk konfigurasi, virtual host, root website, dan log
- Cara reload vs restart Nginx
- 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.