如何优化Nginx与PHP-FPM的性能?

Nginx

1 准备环境

1.1 环境信息

web Server = www.cmdschool.org
web Client = xxx

1.1 安装压力测试工具

In Web Client

yum install -y httpd-tools

1.2 使用工具测试服务器

In Web Client

ab -c 1000 -n 1000 https://www.cmdschool.org

2 Too many open files的错误处理

2.1 错误提示

tail -f /var/log/nginx/error.log

可见如下错误,

2019/03/13 09:24:16 [alert] 8090#8090: *4589 socket() failed (24: Too many open files) while connecting to upstream, client: 10.168.0.23, server: www.cmdschool.org, request: "GET / HTTP/1.0", upstream: "http://10.168.0.110:80/", host: "www.cmdschool.org"

注:以上错误信息属于代理服务器

2.2 修改nginx的性能参数

vim /etc/nginx/nginx.conf

定义以下配置,

worker_processes  2;
worker_cpu_affinity 01 10;
worker_rlimit_nofile 1024000;
worker_priority -5;

events {
    worker_connections  102400;
}

2.3 检查配置符合要求

nginx -t

2.4 重启服务使配置生效

systemctl restart nginx

2.5 监视日志

tail -f /var/log/nginx/error.log

2.6 再次发起压力测试

ab -c 1000 -n 1000 https://www.cmdschool.org

2.7 没有使用的配置方法(可能有用)

2.7.1 设置limit

vim /etc/security/limits.d/21-nginx.conf

加入如下设置,

nginx         soft    nofile    102400
nginx         hard    nofile    102400
root          soft    nofile    102400
root          hard    nofile    102400

2.7.2 测试limit

su nginx -lc 'ulimit -n'
su root -lc 'ulimit -n'

3 Connection reset by peer错误

3.1 Nginx的错误提示

tail -f /var/log/nginx/error.log

发现如下错误提示,

2019/03/13 13:25:45 [error] 22583#22583: *345857 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 10.168.0.169, server: localhost, request: "GET / HTTP/1.0", upstream: "fastcgi://127.0.0.1:9000", host: "www.cmdschool.org"

注:以上错误信息属于nginx搭建的Web服务器

3.2 php-fpm警告提示

tail -f /var/log/php-fpm/error.log

可发现如下警告记录,

[13-Mar-2019 13:07:04] WARNING: [pool www] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 16 children, there are 0 idle, and 47 total children
[13-Mar-2019 13:07:05] WARNING: [pool www] server reached pm.max_children setting (50), consider raising it

注:以上错误信息属于nginx的后端PHP-FPM

3.3 修改允许的子进程数量

vim /etc/php-fpm.d/www.conf

修改如下配置,

; Choose how the process manager will control the number of child processes.
; Possible Values:
;   static  - a fixed number (pm.max_children) of child processes;
;   dynamic - the number of child processes are set dynamically based on the
;             following directives:
;             pm.max_children      - the maximum number of children that can
;                                    be alive at the same time.
;             pm.start_servers     - the number of children created on startup.
;             pm.min_spare_servers - the minimum number of children in 'idle'
;                                    state (waiting to process). If the number
;                                    of 'idle' processes is less than this
;                                    number then some children will be created.
;             pm.max_spare_servers - the maximum number of children in 'idle'
;                                    state (waiting to process). If the number
;                                    of 'idle' processes is greater than this
;                                    number then some children will be killed.
; Note: This value is mandatory.
pm = dynamic
pm.max_children = 100
pm.start_servers = 40
pm.min_spare_servers = 5
pm.max_spare_servers = 70

3.4 测试配置是否合法

php-fpm -t

3.5 重载使服务生效

systemctl reload php-fpm

3.6 同时监视nginx与php-fpm的日志

tail -f /var/log/nginx/error.log
tail -f /var/log/php-fpm/error.log

3.7 再次发起压力测试

ab -c 1000 -n 1000 https://www.cmdschool.org

4 其他辅助命令

top
iostat
iftop
free -lh

注:使用以上命令随时监控压力测试带来的CPU、内存或硬盘IO等资源的变化

参阅文档:
==================
https://www.cmdschool.org/archives/1964

没有评论

发表回复

Nginx
如何使用Nginx发布维护公告?

1 前言 一个问题,一篇文章,一出故事。 最近由于需要发布维护公告,想着这种东西以后用得着,于是总结 …

Nginx
如何实现Nginx第一次访问跳转?

1 前言 一个问题,一篇文章,一出故事。 笔者在NextCloud生产环境中需要实现首次登录显示公告 …

Nginx
如何实现Nginx首次访问跳转?

1 前言 一个问题,一篇文章,一出故事。 笔者在NextCloud生产环境中需要实现首次登录显示公告 …