Dalam dunia web modern, Nginx telah menjadi web server pilihan utama untuk menangani trafik tinggi, arsitektur microservice, WordPress, CodeIgniter, hingga API Node.js. Namun, performa Nginx tidak otomatis optimal hanya dengan instalasi default. Diperlukan konfigurasi yang tepat, seimbang antara keamanan, kecepatan, dan efisiensi resource.
Artikel ini membahas konfigurasi Nginx HTTP dan HTTPS yang telah dioptimasi untuk produksi, lengkap dengan alasan teknis di balik setiap bagian, sehingga bisa dijadikan referensi jangka panjang.
Contents
- 1 1. Filosofi Konfigurasi
- 2 2. Konfigurasi HTTP (Port 80)
- 3 3. HTTPS Server (Port 443 – Optimized)
- 4 4. Root dan Index
- 5 5. SSL Configuration (Minimal & Cepat)
- 6 6. OCSP Stapling Dimatikan
- 7 7. HSTS (Keamanan Browser)
- 8 8. Logging untuk Performa
- 9 9. Hardening Security Dasar
- 10 10. Static Files (Super Fast)
- 11 11. Front Controller (WP & CI)
- 12 12. PHP-FPM High Performance
- 13 13. Routing CodeIgniter Khusus
- 14 14. Node.js API sebagai Reverse Proxy
- 15 15. GZIP Compression
- 16 16. Kesimpulan
- 17 Lampiran: Konfigurasi Nginx Lengkap (Final & Production-Ready)
- 18 Catatan Akhir
- 19 Related Posts
1. Filosofi Konfigurasi
Tujuan utama konfigurasi ini adalah:
- HTTPS wajib (HTTP hanya sebagai redirect)
- Latency rendah
- Penggunaan RAM efisien
- Aman untuk WordPress & CodeIgniter
- Mendukung API Node.js
- Minim warning dan overhead
Pendekatan ini cocok untuk:
- VPS kecil–menengah
- Shared workload (WP + CI + Node)
- Traffic stabil hingga tinggi
- Lingkungan produksi nyata
2. Konfigurasi HTTP (Port 80)
langkah awal buat folder:
Jalankan command ini:
mkdir -p /var/www/certbot
Kalau dipakai oleh Nginx / Certbot, set permission aman:
chown -R www-data:www-data /var/www/certbot
chmod -R 755 /var/www/certbot
server {
listen 80;
server_name seosatu.com www.seosatu.com;
location /.well-known/acme-challenge/ {
root /var/www/certbot;
}
location / {
return 301 https://$host$request_uri;
}
}
2.1 Fungsi Utama
- Melayani ACME challenge untuk Certbot (Let’s Encrypt)
- Redirect 301 permanen ke HTTPS
2.2 Kenapa Tetap Butuh Port 80?
- Let’s Encrypt wajib mengakses HTTP untuk validasi
- SEO membutuhkan redirect 301 yang konsisten
- Beberapa firewall atau client lama masih memulai dari HTTP
2.3 Kenapa Redirect di location /?
Lebih efisien daripada rewrite global, dan memastikan challenge ACME tidak ikut ter-redirect.
3. HTTPS Server (Port 443 – Optimized)
server {
listen 443 ssl http2 reuseport backlog=65535;
server_name seosatu.com www.seosatu.com;
3.1 Penjelasan Directive
ssl→ Aktifkan TLShttp2→ Multiplexing, header compression, latency lebih rendahreuseport→ Multi-core accept queue (lebih scalable)backlog=65535→ Antisipasi lonjakan koneksi
Ini kombinasi high concurrency friendly.
4. Root dan Index
root /var/www/seosatu.com;
index index.php index.html;
- Mendukung PHP-first
- Cocok untuk WordPress & CodeIgniter
- Tidak memaksa index.html lebih dulu
5. SSL Configuration (Minimal & Cepat)
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256';
ssl_prefer_server_ciphers on;
5.1 Kenapa Cipher Minimal?
- AES-128 GCM = lebih cepat dari AES-256
- Aman secara kriptografi
- Mengurangi CPU overhead TLS handshake
5.2 Session Cache
ssl_session_cache shared:SSL:50m;
ssl_session_timeout 1d;
ssl_session_tickets off;
- Mengurangi full handshake berulang
- Session ticket dimatikan untuk keamanan + stabilitas
- Cache 50MB cukup untuk ribuan koneksi simultan
6. OCSP Stapling Dimatikan
ssl_stapling off;
ssl_stapling_verify off;
Alasan:
- Menghindari latency tambahan
- Menghilangkan warning Nginx
- Browser modern sudah soft-fail OCSP
- Lebih stabil di VPS kecil
Keamanan tidak berkurang.
7. HSTS (Keamanan Browser)
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
- Memaksa browser selalu HTTPS
- Mencegah downgrade attack
- Aman karena HTTP sudah redirect total
8. Logging untuk Performa
access_log off;
error_log /var/log/nginx/seosatu.com.error.log warn;
Kenapa access_log dimatikan?
- Logging = disk I/O
- Untuk high traffic → bottleneck
- Monitoring bisa dilakukan via application atau metrics lain
9. Hardening Security Dasar
location ~ /\. { deny all; }
location ~* \.(env|log|ini|conf|sql|bak)$ { deny all; }
location ~* wp-config.php { deny all; }
Mencegah:
- Bocornya file sensitif
- Akses konfigurasi
- Serangan reconnaissance
10. Static Files (Super Fast)
location ~* \.(css|js|png|jpg|jpeg|gif|ico|woff2?|svg)$ {
expires 365d;
access_log off;
log_not_found off;
add_header Cache-Control "public, max-age=31536000, immutable";
try_files $uri =404;
}
Dampak:
- Cache browser maksimal
- Hampir nol request ulang
- Sangat SEO & performance friendly
11. Front Controller (WP & CI)
location / {
try_files $uri $uri/ /index.php?$args;
}
- Standar WordPress
- Kompatibel CodeIgniter 3
- Tidak memicu PHP jika file statis ada
12. PHP-FPM High Performance
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
Buffer Super Hemat tapi Aman
fastcgi_buffers 6 8k; # total 48k
fastcgi_buffer_size 16k;
fastcgi_busy_buffers_size 24k;
fastcgi_temp_file_write_size 24k;
Ini:
- Lolos aturan internal Nginx
- Hemat RAM
- Stabil untuk PHP modern
Timeout diperbesar agar aman untuk proses berat.
13. Routing CodeIgniter Khusus
location /rekrutmenID/ {
try_files $uri $uri/ /rekrutmenID/index.php?$query_string;
}
- Mendukung multi-app CI
- Tanpa rewrite kompleks
- Aman dan eksplisit
14. Node.js API sebagai Reverse Proxy
location /api/apk/ {
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
Kenapa Penting?
- Menjaga keep-alive
- IP client asli tetap terbaca
- Cocok untuk Express / Fastify
proxy_pass http://127.0.0.1:7000/api/apk/;
Tidak ada rewrite berlebih → efisien.
15. GZIP Compression
gzip on;
gzip_comp_level 6;
gzip_min_length 1024;
- Level 6 = seimbang CPU vs ukuran
- Aktif hanya untuk konten teks
- Tidak memproses file kecil
16. Kesimpulan
Konfigurasi ini adalah contoh Nginx production-grade yang realistis, bukan sekadar “best practice di atas kertas”.
Keunggulan:
- 🔥 Cepat
- 🔒 Aman
- 🧠 Hemat RAM
- ⚙️ Multi stack (WP + CI + Node)
- 🚀 Siap traffic tinggi
Jika ingin melangkah lebih jauh:
- FastCGI Cache
- Brotli
- HTTP/3
- CDN integration
- PHP-FPM tuning
Namun untuk baseline produksi, konfigurasi ini sudah sangat solid.
Lampiran: Konfigurasi Nginx Lengkap (Final & Production-Ready)
Konfigurasi berikut merupakan hasil akhir optimasi, mencakup:
- HTTP → HTTPS redirect
- SSL minimal & cepat
- WordPress + CodeIgniter
- PHP-FPM hemat RAM
- Node.js API reverse proxy
- Static cache agresif
- Logging minimal
- Tanpa OCSP stapling (latency stabil)
######################################
# HTTP
######################################
server {
listen 80;
server_name seosatu.com www.seosatu.com;
# ACME challenge (Certbot)
location /.well-known/acme-challenge/ {
root /var/www/certbot;
}
# Force HTTPS
location / {
return 301 https://$host$request_uri;
}
}
######################################
# HTTPS (OPTIMIZED)
######################################
server {
listen 443 ssl http2 reuseport backlog=65535;
server_name seosatu.com www.seosatu.com;
root /var/www/seosatu.com;
index index.php index.html;
#### SSL ####
ssl_certificate /etc/letsencrypt/live/seosatu.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/seosatu.com/privkey.pem;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256';
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:50m;
ssl_session_timeout 1d;
ssl_session_tickets off;
# OCSP stapling disabled (latency-stable)
ssl_stapling off;
ssl_stapling_verify off;
# HSTS
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
#### LOGGING (PERFORMANCE) ####
access_log off;
error_log /var/log/nginx/seosatu.com.error.log warn;
#### SECURITY ####
location ~ /\. {
deny all;
}
location ~* \.(env|log|ini|conf|sql|bak)$ {
deny all;
}
location ~* wp-config.php {
deny all;
}
#### STATIC FILES (SUPER FAST) ####
location ~* \.(css|js|png|jpg|jpeg|gif|ico|woff2?|svg)$ {
expires 365d;
access_log off;
log_not_found off;
add_header Cache-Control "public, max-age=31536000, immutable";
try_files $uri =404;
}
#### WORDPRESS / CI FRONT CONTROLLER ####
location / {
try_files $uri $uri/ /index.php?$args;
}
#### PHP-FPM (HIGH PERFORMANCE & LOW RAM) ####
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/run/php/php7.4-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
# Optimized buffers (hemat RAM, valid rule)
fastcgi_buffers 6 8k; # total 48k
fastcgi_buffer_size 16k;
fastcgi_busy_buffers_size 24k; # < (6-1)*8k = 40k
fastcgi_temp_file_write_size 24k;
fastcgi_connect_timeout 60s;
fastcgi_send_timeout 180s;
fastcgi_read_timeout 180s;
}
#### CODEIGNITER ROUTES ####
location /rekrutmenID/ {
try_files $uri $uri/ /rekrutmenID/index.php?$query_string;
}
location /buzzerID/ {
try_files $uri $uri/ /buzzerID/index.php?$query_string;
}
#### NODE.JS API (OPTIMIZED REVERSE PROXY) ####
location /api/apk/ {
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_read_timeout 60s;
proxy_pass http://127.0.0.1:7000/api/apk/;
}
#### GZIP COMPRESSION ####
gzip on;
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_min_length 1024;
gzip_http_version 1.1;
gzip_types
text/plain
text/css
application/javascript
application/json
application/xml
application/xml+rss
text/javascript;
}
Catatan Akhir
Konfigurasi ini:
- ✅ Lolos
nginx -t - ✅ Stabil untuk produksi
- ✅ Hemat RAM
- ✅ Latency rendah
- ✅ Multi-stack ready (WP + CI + Node.js)