# ========================================================= # Request ID for correlation # ========================================================= map $http_x_request_id $req_id { default $http_x_request_id; "" $request_id; } # ========================================================= # Rate limiting # ========================================================= limit_req_zone $binary_remote_addr zone=api:10m rate=30r/m; limit_req_zone $binary_remote_addr zone=uploads:10m rate=10r/m; # ========================================================= # Upstream backend # ========================================================= upstream backend { server 127.0.0.1:{{ backend_local_port }} max_fails=3 fail_timeout=30s; keepalive 32; keepalive_requests 100; keepalive_timeout 60s; } # ========================================================= # API Server # ========================================================= server { listen {{ backend_port }}; listen [::]:{{ backend_port }}; server_name {{ ip }}; client_max_body_size {{ nginx_max_body_size }}; server_tokens off; # ========================================================= # Security headers # ========================================================= add_header X-Frame-Options "DENY" always; add_header X-Content-Type-Options "nosniff" always; add_header Referrer-Policy "strict-origin-when-cross-origin" always; proxy_hide_header X-Powered-By; proxy_hide_header Server; # ========================================================= # API proxy # ========================================================= location / { limit_req zone=api burst=20 nodelay; limit_except GET POST PUT PATCH DELETE OPTIONS { deny all; } proxy_pass http://backend; proxy_http_version 1.1; 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_set_header X-Forwarded-Proto $scheme; proxy_set_header X-Request-ID $req_id; proxy_set_header Connection ""; proxy_connect_timeout 60s; proxy_send_timeout 60s; proxy_read_timeout 60s; } # ========================================================= # Uploads # ========================================================= location /uploads/ { alias {{ uploads_dir }}/; limit_req zone=uploads burst=20 nodelay; limit_except GET HEAD { deny all; } # Bloquer les fichiers exécutables location ~* \.(php|php5|phtml|sh|py|pl|cgi|exe|asp|aspx|jsp)$ { deny all; } expires 30d; add_header Cache-Control "public, immutable"; add_header X-Content-Type-Options "nosniff" always; } location ^~ /uploads/images/ { alias {{ uploads_dir }}/images/; limit_req zone=uploads burst=20 nodelay; limit_except GET HEAD OPTIONS { deny all; } add_header Access-Control-Allow-Origin "*" always; add_header Access-Control-Allow-Methods "GET, HEAD, OPTIONS" always; add_header X-Content-Type-Options "nosniff" always; expires 30d; add_header Cache-Control "public, immutable" always; types { image/jpeg jpg jpeg; image/png png; image/gif gif; image/webp webp; image/svg+xml svg; } default_type image/jpeg; } location ^~ /uploads/videos/ { alias {{ uploads_dir }}/videos/; limit_req zone=uploads burst=20 nodelay; limit_except GET HEAD OPTIONS { deny all; } add_header Access-Control-Allow-Origin "*" always; add_header Access-Control-Allow-Methods "GET, HEAD, OPTIONS" always; add_header X-Content-Type-Options "nosniff" always; expires 30d; add_header Cache-Control "public, immutable" always; types { video/mp4 mp4; video/webm webm; video/ogg ogv; } default_type video/mp4; } # ========================================================= # Deny hidden files and sensitive files # ========================================================= location ~ /\. { deny all; access_log off; log_not_found off; } location ~* (\.env|\.git|package\.json|package-lock\.json|yarn\.lock|Dockerfile|docker-compose\.yml)$ { deny all; access_log off; log_not_found off; } }