系统重启后ngix reload不生效原因分析
系统重启后ngix reload不生效原因分析这是一种比较少见,困扰我很久的问题,虽然这个问题很简单,但是找到根本原因还是费了不少时间,现在把分析过程分享如下。
前提:需要对Linux系统启动过程、Nginx进程启动过程及进程跟踪有一定的理解。
一,Nginx reload过程分析:
经过查看官网文档及结合Nginx源码分析,大致得出reload过程进行了如下操作。
1,检查配置是否正确
相当于nginx -t
2,打开日志文件
相当于nginx -s reopen
由于日志文件比较多,需要打开多个文件
3,重新监听套接字
相当于nginx
这个步骤会初始化很多东西,重点关注哈希表
4,关闭旧worker进程
相当于nginx -s quit
二,nginx进程分析
1,首先了解nginx的两种进程
master进程,root用户打开,接收信号,管理worker进程
worker进程,nginx用户打开,工作进程,负责处理http请求
2,starce跟踪主进程号,期间执行nginx -s reload,发现卡在检查日志文件这块
主进程跟踪,因为reload过程是系统发送HUP信号给nginx主进程
#starce -p 2298
......
open("/data/wwwlogs/access.xxx.xxx.xxx.log", O_WRONLY|O_CREAT|O_APPEND, 0644) = -1 EMFILE (Too many open files)
write(808, "2016/02/17 09:50:22 [emerg] 2298"..., 124) = 124
......
3,根据提示,查找进程的系统限制文件
master进程限制
# cat /proc/2398/limits
Limit Soft Limit Hard Limit Units
Max cpu time unlimited unlimited seconds
Max file size unlimited unlimited bytes
Max data size unlimited unlimited bytes
Max stack size 10485760 unlimited bytes
Max core file size 0 unlimited bytes
Max resident set unlimited unlimited bytes
Max processes 127015 127015 processes
Max open files 1024 4096 files
Max locked memory 65536 65536 bytes
Max address space unlimited unlimited bytes
Max file locks unlimited unlimited locks
Max pending signals 127015 127015 signals
Max msgqueue size 819200 819200 bytes
Max nice priority 0 0
Max realtime priority 0 0
Max realtime timeout unlimited unlimited us
worker进程限制
# cat /proc/2300/limits
Limit Soft Limit Hard Limit Units
Max cpu time unlimited unlimited seconds
Max file size unlimited unlimited bytes
Max data size unlimited unlimited bytes
Max stack size 10485760 unlimited bytes
Max core file size 0 unlimited bytes
Max resident set unlimited unlimited bytes
Max processes 127015 127015 processes
Max open files 409600 409600 files
Max locked memory 65536 65536 bytes
Max address space unlimited unlimited bytes
Max file locks unlimited unlimited locks
Max pending signals 127015 127015 signals
Max msgqueue size 819200 819200 bytes
Max nice priority 0 0
Max realtime priority 0 0
Max realtime timeout unlimited unlimited us
补充错误日志:
2016/02/17 10:48:05 [notice] 47386#0: signal process started
2016/02/17 10:48:05 [emerg] 2298#0: open() "/data/wwwlogs/access_xxx.xxx.xxx.log" failed (24: Too many open files)
三,解决方案
1,修改限制
一般从以下3方面调优:
第一:nginx.conf参数规划与设置
worker_rlimit_nofile :限制单个工作进程打开的最大文件数:
线上配置没有问题
worker_rlimit_nofile 409600;
第二:系统级别的检查与设置
就是 /etc/security/limits.conf的配置与修改,请参考Linux系统资源限制汇总
线上配置没有问题
* soft nofile 655350
* hard nofile 655350
第三:内核级别的检查与设置:
fs.file-max值的大小设置:
线上配置比较大
fs.file-max = 6553600
注意:file-max的默认值大概是系统内存的10%(系统内存以kb计算)
2,验证生效
结果发现以上配置前期都有配置,但是重启服务器发现主进程的限制并没有修改过来,但是登陆服务器后无论在终端ulimit -n 查看还是关闭nginx主进程后重启nginx都生效了,由此推理出
问题可能出在linux系统启动过程中,也就是说nginx主进程启动时,上面的限制配置没有生效,后来查阅资料发现系统启动后执行login时才会使limits.conf配置生效,所以需要调整顺序。
根据实际情况,系统启动过程如下:
1、读取/etc/inittab来读取默认级别 假设:读取到的默认级别是 3
2、执行初始化系统脚本 /etc/rc.d/rc.sysinit 来初始化脚本
3、然后执行 /etc/rc.d/rc 脚本
4、执行/etc/rc.d/rc.local脚本,此脚本是启动过程中最后启动的一个脚本。
最后会执行 /bin/login 登录用户。至此系统启动过程完成,login时才会执行/etc/profile,~/.bash_profile和~/.bashrc等,此时的ulimit -n查到的值不是nginx进程启动时的值。
默认用户登陆时会使limits.conf配置文件生效,这个比nginx进程启动晚,要在这之前使配置生效,需要补充配置如下:
cat /etc/rc.local
ulimit -HSn 655350 (注意在nginx启动前执行)
/usr/local/nginx/sbin/nginx
四,补充优化
主要是相关参数调大了一些。
1,内核优化
net.ipv4.tcp_max_tw_buckets 修改大一些,减少内核负担,iptable本身对内核性能有影响
# ss -an |awk '{print $1}'|sort |uniq -c |sort -rn
15415 ESTAB
12979 TIME-WAIT
1961 FIN-WAIT-2
501 FIN-WAIT-1
234 LAST-ACK
32 SYN-RECV
11 LISTEN
3 CLOSING
1 SYN-SENT
1 State
1 CLOSE-WAIT
线上修改配置如下:
net.ipv4.tcp_max_tw_buckets = 18000
2,nginx优化
主要是哈希表,其他配置已经优化,哈希表有如下几种
server_names_hash可以加
map_hash可以加
types_hash够用
request header 不考虑
variables_hash 够用
线上修改配置如下:
server_names_hash_max_size 512000;
server_names_hash_bucket_size 64; (默认)
map_hash_max_size 204800;
map_hash_bucket_size 64; (默认)

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

In PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment