nginx+php-fpm 故障排查及优化

2020-6-27 Frank PHP

nginx+php-fpm 故障排查及优化

网站突然报警,访问发现 504 错误,后端连接超时了,php-fpm 参数调优没起作用,最后发现是慢查询导致的。

修改 linux 最大打开数

# 临时生效
ulimit -SHn 65535
ulimit -a
# 永久生效
echo "* soft nofile 65535"  >> /etc/security/limits.conf
echo "* hard nofile 65535"  >> /etc/security/limits.conf
#验证
ulimit -n

nginx 配置

首先查看 nginx 访问及错误日志

tail -f /var/log/nginx/error.log
tail -f /data/logs/nginx/xxx_access.log
tail -f /data/logs/nginx/xxx_error.log

相关配置

server {
  listen 80;
  server_name xxx.com;
  rewrite ^ https://xxx.com$request_uri? permanent;
}

server {
  listen 443 ssl;
  server_name  xxx.com;

  large_client_header_buffers 4 16k;     # 读取大型客户端请求头的缓冲区的最大数量和大小
  client_max_body_size 300m;     #设置nginx能处理的最大请求主体大小。
  client_body_buffer_size 128k;  #请求主体的缓冲区大小。
  proxy_connect_timeout 600;
  proxy_read_timeout 600;
  proxy_send_timeout 600;
  proxy_buffer_size 64k;
  proxy_buffers   4 32k;
  proxy_busy_buffers_size 64k;
  proxy_temp_file_write_size 64k;

  ssl_certificate   /etc/nginx/sslkey/xxx.com.pem;
  ssl_certificate_key  /etc/nginx/sslkey/xxx.com.key;
  ssl_session_timeout 5m;
  ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  ssl_ciphers AESGCM:ALL:!DH:!EXPORT:!RC4:+HIGH:!MEDIUM:!LOW:!aNULL:!eNULL;
  ssl_prefer_server_ciphers on;

  access_log    /data/logs/nginx/xxx.com_access.log;
  error_log    /data/logs/nginx/xxx.com_error.log;
  set        $root    /data/www/xxx.com;
  root $root;
  error_page  404              /404.html;
  location = /404.html {
    return 404 'Sorry, File not Found!';
  }
  error_page  500 502 503 504  /50x.html;

  location = /50x.html {
    root   $root;
  }

  location / {
      index  index.html index.htm index.php;
      #autoindex  on;
  }
  if (!-e $request_filename){
      rewrite ^(.*)$ /index.php break;
  }


  location ~ \.php/ {
    if ($request_uri ~ ^(.+\.php)(/.+?)($|\?)) { }
    fastcgi_pass 127.0.0.1:9000;
    include fastcgi_params;
    fastcgi_param SCRIPT_NAME     $1;
    fastcgi_param PATH_INFO       $2;
    fastcgi_param SCRIPT_FILENAME $document_root$1;
  }

  location ~ \.php$ {
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
  }

  location ~ /\.ht {
    deny  all;
  }
}

重启

nginx -s reload

php-fpm 配置说明

查看错误日志

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

编辑配置文件
vim /etc/php-fpm.d/www.conf

pm = static
; Used when pm is set to 'static', 'dynamic' or 'ondemand'
; Note: This value is mandatory.
pm.max_children = 500
; Default Value: min_spare_servers + (max_spare_servers - min_spare_servers) / 2
pm.start_servers = 100
; The desired minimum number of idle server processes.
; Note: Used only when pm is set to 'dynamic'
; Note: Mandatory when pm is set to 'dynamic'
pm.min_spare_servers = 10

; The desired maximum number of idle server processes.
; Note: Used only when pm is set to 'dynamic'
; Note: Mandatory when pm is set to 'dynamic'
pm.max_spare_servers = 500

修改配置重启之前先检查一下语法

/usr/sbin/php-fpm -t

重启

service php-fpm restart

优化之后未见效,开启慢日志

; Default Value: not set
; Note: slowlog is mandatory if request_slowlog_timeout is set
slowlog = /var/log/php-fpm/www-slow.log

; The timeout for serving a single request after which a PHP backtrace will be
; dumped to the 'slowlog' file. A value of '0s' means 'off'.
; Available units: s(econds)(default), m(inutes), h(ours), or d(ays)
; Default Value: 0
request_slowlog_timeout = 10

查看超过十秒的慢日志,有很多。。。
tail -f /var/log/php-fpm/www-slow.log

只有一句echo '11';的文件访问也是 504, 所以开始没想是 mysql 慢查询导致。

mysql 慢查询

通过查看 mysql 慢查询日志,发现有很多三十几秒的查询,加索引后解决
参见
centos mysql 慢查询、超时、连接数修改

推荐大家用宝塔,省去了自己配置,而且相关配置都有详细说明。

参考

linux 最大打开数

发表评论 登录

Top