以下是Debian Nginx的高效配置要點,涵蓋性能、安全及可維護性:
基礎配置優化
worker_processes auto;
(自動匹配CPU核心數)。use epoll;
(Linux高效I/O模型)。worker_connections 4096;
(根據服務器資源調整)。性能優化
sendfile on; tcp_nopush on; tcp_nodelay on;
。gzip on; gzip_types text/css application/javascript;
。expires 30d; add_header Cache-Control "public";
。負載均衡與安全
upstream
模塊配置輪詢、IP哈希等算法。server_tokens off;
(隱藏版本號)、add_header X-Frame-Options "SAMEORIGIN";
。高級功能配置
listen 443 ssl http2;
(啟用HTTP/2提升性能)。proxy_cache_path
配置代理緩存,減少后端壓力。監控與維護
access.log
和error.log
,使用logrotate
切割日志。top
、htop
等工具監控CPU/內存使用情況。配置后需通過nginx -t
測試語法,systemctl reload nginx
重新加載配置。具體參數需根據服務器硬件和業務場景調整。