linux 並發問題accept() failed (24: Too many open files)


centos5.3 中 ulimit -n 為1024, 當Nginx連接數超過1024時,error.log中就出現以下錯誤:

[alert] 12766#0: accept() failed (24: Too many open files)

使用 ulimit -n 655350 可以把打開文件數設置足夠大, 同時修改nginx.conf , 添加 worker_rlimit_nofile 655350; (與error_log同級別)

這樣就可以解決Nginx連接過多的問題,Nginx就可以支持高並發。<還要修改nginx>

另外, ulimit -n 還會影響到mysql 的並發連接數。把他提高,也就提高了mysql並發。

注意: 用ulimit -n 2048 修改只對當前的shell有效,退出后失效。

修改方法

若要令修改ulimits的數值永久生效,則必須修改配置文檔,可以給ulimit修改命令放入/etc/profile里面,這個方法實在是不方便,

還有一個方法是修改/etc/security/limits.conf

/etc/security/limits.conf 格式,文件里面有很詳細的注釋,比如

* soft nofile 655360

* hard nofile 655360

星號代表全局, soft為軟件,hard為硬件,nofile為這里指可打開文件數。

把以上兩行內容加到 limits.conf文件中即可。

另外,要使 limits.conf 文件配置生效,必須要確保 pam_limits.so 文件被加入到啟動文件中。查看 /etc/pam.d/login 文件中有:

session required /lib/security/pam_limits.so
但是linux 64位 pam_limits.so 文件是在 /lib64/security/ 下面 一定要注意
修改完重新登錄就可以見到效果,可以通過 ulimit -n 查看。
在/etc 下sysctl.conf 里增加 
fs.file-max = 655360
查看最大文件
cat /proc/sys/fs/file-max

 

nginx配置

worker_rlimit_nofile 655350;
events {
    use epoll;
    worker_connections 655350;
}
http {

 

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM