Nginx, statik dosyaları PHP'den çok daha hızlı sunar. Doğru yapılandırma, uygulama sunucusunun yükünü önemli ölçüde azaltır.
Temel yapılandırma
server {
listen 443 ssl;
server_name site.com;
root /var/www/site/public;
# Statik dosyalar
location ~* \.(jpg|jpeg|png|gif|webp|avif|ico|svg|woff|woff2|ttf|eot)$ {
expires 1y;
add_header Cache-Control "public, immutable";
access_log off; # log yazmaz, hiz kazandirir
try_files $uri =404;
}
location ~* \.(css|js)$ {
expires 1y;
add_header Cache-Control "public, immutable";
# gzip veya brotli
gzip_static on; # .gz dosyasi varsa onu sun
access_log off;
}
location / {
try_files $uri $uri/ /index.php?$query_string;
}
}
Sıkıştırma
gzip on;
gzip_vary on;
gzip_comp_level 5;
gzip_min_length 512;
gzip_proxied any;
gzip_types
text/plain
text/css
text/xml
application/javascript
application/json
application/xml
application/rss+xml
image/svg+xml;
font/ttf
font/otf;
# ZATEN sikistirilmis formatlar listelenmez:
# image/jpeg, image/png, image/webp, video/mp4
brotli on;
brotli_types text/css application/javascript application/json image/svg+xml;
brotli_comp_level 5;
Öncelikli sıkıştırma (brotli/gzip)
# Brotli ile onceden sikistir
find public/ -type f \( -name '*.css' -o -name '*.js' \) \
-exec brotli -k -f -9 {} \;
# Veya nginx modulunden (onbellekli, ilk istekten sonra)
brotli_static on;
Önbellekleme
proxy_cache_path /var/cache/nginx
levels=1:2
keys_zone=page:50m
max_size=1g
inactive=7d
use_temp_path=off;
location / {
proxy_cache page;
proxy_cache_valid 200 301 302 10m;
proxy_cache_valid 404 1m; # 404'leri de kisa onbellekle
proxy_cache_use_stale error timeout updating http_500 http_502 http_503;
proxy_cache_background_update on; # arka planda yenile
proxy_cache_lock on; # stampede onlemi
proxy_cache_revalidate on;
proxy_cache_key "$scheme$request_method$host$request_uri";
proxy_pass http://127.0.0.1:8000;
}
Dosya tipi güvenliği
# YALNIZCA izin verilen tipler sunulsun
location ~* \.(php|phtml|phar)$ {
# Bunlar ASLA statik olarak sunulmamali
return 404;
}
# Gizli dosyalar
location ~ /\.(?!well-known) {
deny all;
access_log off;
log_not_found off;
}
# Yedek dosyalar
location ~* \.(bak|sql|log|ini|sh|env)$ {
deny all;
}
Önemli: .env, composer.json, .git klasörü web root'ta olmamalıdır. Yoksa try_files ile servis edilebilir.
Gerçek IP
set_real_ip_from 10.0.0.0/8;
set_real_ip_from 173.245.48.0/20; # Cloudflare
set_real_ip_from 103.21.244.0/22;
set_real_ip_from 103.22.200.0/22;
real_ip_header CF-Connecting-IP;
real_ip_recursive on;
Sık yapılan hatalar
- Gzip'i zaten sıkıştırılmış formatlara uygulamak (boyutu büyütür).
.htaccess kurallarını .env'e taşımamak.
- Statik dosyalar için
access_log açık bırakmak (disk I/O).
- PHP dosyalarını statik sunmaya çalışmak.
immutable kullanıp dosya adını hash'lememek.
Sonuç
Statik dosyaları doğru sunmak, PHP'ye hiç uğramadan çok sayfa isteğini karşılar. Bu, basit bir yapılandırma değişikliğiyle yüksek trafikte en büyük kazancı sağlar.
Arivo'da PHP scriptler, temalar ve eklentiler bulun. Kurulum desteği dahil.
Mağazaya Git