高併發服務(Nginx、Redis、Tsung 壓測、Java 應用)常因「Too many open files」報錯而崩潰。Linux 對文件描述符的限制分為內核級、用戶級、進程級三個層面,配置分散在多處。本文系統梳理四層配置體系,提供生產環境標準化調優方案與驗證腳本。
┌─────────────────────────────────────┐
│ 內核級全局上限 /proc/sys/fs/file-max │ ← 系統總上限
├─────────────────────────────────────┤
│ 用戶級軟/硬限制 /etc/security/limits.conf │ ← 登錄會話生效
├─────────────────────────────────────┤
│ 進程級當前限制 ulimit -n / /proc/PID/limits │ ← 實際生效值
└─────────────────────────────────────┘
/proc/sys/fs/file-max# 臨時生效
echo 2000000 > /proc/sys/fs/file-max
# 永久生效
echo "fs.file-max = 2000000" >> /etc/sysctl.conf
sysctl -p
查看當前值: cat /proc/sys/fs/file-max 或 sysctl fs.file-max
/etc/security/limits.conf# 格式: -
# * 代表所有用戶,root 亦受限(除非另有配置)
* soft nofile 1000000
* hard nofile 1000000
# 針對特定服務用戶(推薦)
redis soft nofile 1000000
redis hard nofile 1000000
nginx soft nofile 1000000
nginx hard nofile 1000000
關鍵點:
soft:當前生效值,用戶可自行調低至 hard 以下hard:上限,僅 root 可提高- 等同於同時設置 soft 和 hard/etc/pam.d/login / /etc/pam.d/sshd# 確保以下行存在且未註釋
session required pam_limits.so
無此行,limits.conf 不生效。現代發佈版通常默認已包含。
# 編輯服務單元
systemctl edit nginx
# 或 mkdir -p /etc/systemd/system/nginx.service.d/ 並創建 override.conf
[Service]
LimitNOFILE=1000000
LimitNPROC=1000000
systemctl daemon-reload
systemctl restart nginx
優先級順序: systemd 服務覆蓋 > limits.conf > 內核默認值
#!/bin/bash
# check-fd-limits.sh
echo "=== 1. 內核全局上限 ==="
sysctl fs.file-max
echo -e "\n=== 2. 當前 Shell 限制 ==="
ulimit -a | grep -E 'open files|max user processes'
echo -e "\n=== 3. limits.conf 配置 ==="
grep -E '^\s*\*|^\s*[a-z]' /etc/security/limits.conf | grep -v '^#'
echo -e "\n=== 4. 關鍵服務實際限制 ==="
for svc in nginx redis mysqld docker; do
PID=$(systemctl show --property=MainPID --value $svc 2>/dev/null)
if [[ $PID -gt 0 ]]; then
echo "--- $svc (PID: $PID) ---"
grep -E 'Max open files|Max processes' /proc/$PID/limits
fi
done
echo -e "\n=== 5. systemd 服務覆蓋 ==="
for svc in nginx redis mysqld docker; do
systemctl show --property=LimitNOFILE,LimitNPROC $svc 2>/dev/null | grep -v '^Limit.*=18446744073709551615$'
done
| 場景 | 建議 file-max | 建議 nofile (soft/hard) | 備註 |
|---|---|---|---|
| Web 服務器 | 1000000 | 1000000 | Nginx worker_connections × worker_processes |
| 數據庫 | 2000000 | 1000000 | MySQL open_files_limit, table_open_cache |
| 消息隊列 | 2000000 | 1000000 | Kafka/RabbitMQ 連接數大 |
| 壓測工具 | 2000000 | 1000000 | Tsung/JMeter 需大量並發連接 |
| 容器宿主機 | 4000000+ | 1000000 | 含容器內開銷,建議更大 |
ulimit -n 65535 僅對當前 Shell 有效,重啟失效ulimit,可用 --ulimit nofile=1000000:1000000 覆蓋-XX:-MaxFDLimit(JDK 9+)或確保 ulimit 生效前啟動 JVMlimits.conf 後,systemd 服務需重啟,systemctl reload 不重讀 limits/etc/sysctl.conf 修改 fs.file-max 需 sysctl -p,且值不得小於當前已分配數原文附帶 MySQL 5.1 安裝步驟,現代環境建議使用發佈版倉庫或官方 RPM:
# CentOS 7/8/9
yum install -y mysql-server # MariaDB 兼容
# 或官方
rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-5.noarch.rpm
yum install -y mysql-community-server
# 關鍵配置 /etc/my.cnf.d/limits.cnf
[mysqld]
open_files_limit = 1000000
max_connections = 2000
核心心法: 文件描述符調優是「桶理論」——最短的那塊板決定上限。按 內核 → PAM → limits.conf → systemd 四層全鏈路配置,再用驗證腳本閉環檢查,才能徹底告別「Too many open files」。