Berikut adalah penjelasan lengkap mengenai file apa saja yang diubah, isi perubahannya, dan langkah-langkah optimasi Nginx di Ubuntu:
/etc/nginx/nginx.conf
worker_processes auto; # Secara otomatis menyesuaikan dengan jumlah core CPU
worker_connections 1024; # Jumlah maksimum koneksi simultan per worker
http
:sendfile on; # Mempercepat pengiriman file
tcp_nopush on; # Meningkatkan efisiensi pengiriman file
tcp_nodelay on; # Memastikan pengiriman data kecil lebih cepat
keepalive_timeout 65; # Waktu tunggu koneksi keep-alive
types_hash_max_size 2048; # Ukuran maksimal untuk hash jenis MIME
gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
gzip_proxied any;
gzip_min_length 1024;
/etc/nginx/sites-available/default
(atau file virtual host lain yang digunakan)location ~* \.(jpg|jpeg|png|gif|ico|css|js|woff|woff2|ttf|svg|eot)$ {
expires max;
log_not_found off;
}
fastcgi_cache_path /etc/nginx/cache levels=1:2 keys_zone=FASTCGI:100m inactive=60m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock; # Sesuaikan versi PHP
fastcgi_cache FASTCGI;
fastcgi_cache_valid 200 301 302 1h;
fastcgi_cache_bypass $http_cache_control;
fastcgi_no_cache $http_cache_control;
}
http2
ke dalam konfigurasi listen
:server {
listen 443 ssl http2;
ssl_certificate /etc/nginx/ssl/your_domain.crt;
ssl_certificate_key /etc/nginx/ssl/your_domain.key;
...
}
/etc/sysctl.conf
net.core.somaxconn = 65535
net.ipv4.tcp_max_syn_backlog = 65535
net.ipv4.tcp_tw_reuse = 1
net.ipv4.ip_local_port_range = 1024 65000
net.ipv4.tcp_fin_timeout = 15
sudo sysctl -p
/etc/security/limits.conf
* soft nofile 65535
* hard nofile 65535
/etc/pam.d/common-session
dan /etc/pam.d/common-session-noninteractive
, tambahkan:session required pam_limits.so
/etc/nginx/ssl/your_domain.crt
/etc/nginx/ssl/your_domain.key
sudo nginx -t
sudo systemctl restart nginx
htop
, ngxtop
, atau Nginx Amplify
untuk memantau performa server.