Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> Linux 教學

Nginx 限制單 IP 並發連接數與日誌優化實戰

編輯:Linux 教學

通過 Nginx 限制單 IP 並發連接可有效緩解采集程序、CC 攻擊;關閉訪問日誌寫入能大幅減少磁盤 I/O,延長硬盤壽命。本文提供具體配置指令與重載方法。

限制單 IP 並發連接

  1. 編輯 /usr/local/nginx/conf/nginx.conf
  2. 在 http 塊中定義限制區域(若已存在但被註釋,取消註釋):
    limit_conn_zone $binary_remote_addr zone=perip:10m;
  3. 在 server 或 location 塊中應用限制:
    limit_conn perip 20;

    含義:每個 IP 最大 20 個並發連接。

  4. 重載生效:nginx -s reload 或 kill -HUP `cat /usr/local/nginx/logs/nginx.pid`

官方模塊文檔

Nginx HttpLimitZoneModule 中文文檔

日誌優化:丟棄訪問/錯誤日誌

# 在 server 塊中設置
access_log /dev/null;
error_log /dev/null;

效果:所有日誌寫入系統黑洞,消除磁盤頻繁讀寫,提升性能並保護硬盤。

適用場景

  • 高併發靜態資源服務器
  • API 網關、反向代理節點
  • 日誌由上層負載均衡器統一收集的節點

注意事項

  • 限制連接數需根據業務 QPS 調整,過小會誤傷正常用戶
  • 關閉日誌將無法進行訪問分析、故障排查,請確認有其他日誌收集機制
  • 錯誤日誌建議保留 error_log /var/log/nginx/error.log warn; 僅記錄警告以上級別
Copyright © Windows教程網 All Rights Reserved