在Ubuntu上對Nginx進行安全設置是確保Web服務器穩定性和數據安全性的重要步驟。以下是一些基本的安全配置措施:
server_tokens off;
以避免在響應頭中暴露Nginx版本號,減少被攻擊的風險。limit_except GET POST { deny all; }
來限制非GET和POST請求,防止惡意請求。add_header X-Frame-Options "SAMEORIGIN";
以防止網頁被嵌套在frame中。add_header X-XSS-Protection "1; mode=block";
啟用瀏覽器XSS過濾。server { listen 80; return 301 https://$host$request_uri; }
實現。ssl_ciphers 'ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384';
,并啟用TLS 1.2和TLS 1.3。allow 192.168.1.0/24; deny all;
僅允許特定IP訪問Nginx服務。limit_req_zone
和 limit_req
指令限制每個IP的請求速率,防止DDoS攻擊。location ~* \.(php|log|env|git|svn|htaccess)$ { deny all; }
禁止訪問敏感文件和目錄。log_format security '$remote_addr - $remote_user [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" "$http_x_forwarded_for"';
記錄關鍵安全信息。通過上述配置,可以顯著提高Nginx服務器的安全性。但請注意,安全是一個持續的過程,需要定期更新、審計和加固。