我的 nginx 不断崩溃并在浏览器中报告“坏网关”错误。Nginx 和 PHP-FPM 没有预先配置来处理大流量负载。我必须systemctl restart php7.0-fpm
每小时安排一次cron 工作,以确保我的网站在运行时不会停留太久。让我们开始吧。
我从中得到的一些错误/var/log/php7.0-fpm.log
:
[20-Sep-2017 12:08:21] NOTICE: [pool web3] child 3495 started
[20-Sep-2017 12:08:21] NOTICE: [pool web3] child 2642 exited with code 0 after 499.814492 seconds from start
[20-Sep-2017 12:32:28] WARNING: [pool web3] seems busy (you may need to increase pm.start_servers, or pm.min/max_spare_servers), spawning 8 children, there are 7 idle, and 57 total children
Run Code Online (Sandbox Code Playgroud)
nginx 日志中没有任何内容让我感到意外。如果我让它运行太长时间而不重新启动它 (PHP-FPM),我会收到网关错误。我已经尝试按照教程 3 次调整设置,但仍然不好。现在我可能已经完成了各种设置,但无论哪种方式都行不通。
/etc/nginx/nginx.conf
:
user www-data;
worker_processes auto;
pid /run/nginx.pid;
worker_rlimit_nofile 100000;
events {
worker_connections 4096;
use epoll;
multi_accept on;
}
http {
sendfile on;
reset_timedout_connection on;
client_body_timeout 10;
send_timeout 2;
keepalive_timeout 30;
keepalive_requests 100000;
tcp_nopush on;
tcp_nodelay on;
types_hash_max_size 2048;
fastcgi_read_timeout 300000;
client_max_body_size 9000m;
include /etc/nginx/mime.types;
default_type application/octet-stream;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
ssl_prefer_server_ciphers on;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
gzip on;
gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
open_file_cache max=200000 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 2;
open_file_cache_errors on;
access_log off;
}
Run Code Online (Sandbox Code Playgroud)
/etc/php/7.0/fpm/php-fpm.conf
:
[www]
pm = dynamic
pm.max_spare_servers = 200
pm.min_spare_servers = 100
pm.start_servers = 100
pm.max_children = 300
[global]
pid = /run/php/php7.0-fpm.pid
error_log = /var/log/php7.0-fpm.log
include=/etc/php/7.0/fpm/pool.d/*.conf
Run Code Online (Sandbox Code Playgroud)
/etc/php/7.0/fpm/pool.d/www.conf
:
[www]
user = www-data
group = www-data
listen = /run/php/php7.0-fpm.sock
listen.owner = www-data
listen.group = www-data
pm = dynamic
pm.max_children = 300
pm.start_servers = 100
pm.min_spare_servers = 100
pm.max_spare_servers = 200
pm.max_requests = 500
Run Code Online (Sandbox Code Playgroud)
我的网站之一 ( /etc/php/7.0/fpm/pool.d/web3.conf
):
[web3]
listen = /var/lib/php7.0-fpm/web3.sock
listen.owner = web3
listen.group = www-data
listen.mode = 0660
user = web3
group = client1
pm = dynamic
pm.max_children = 141
pm.start_servers = 20
pm.min_spare_servers = 20
pm.max_spare_servers = 35
pm.max_requests = 500
chdir = /
env[HOSTNAME] = $HOSTNAME
env[TMP] = /var/www/clients/client1/web3/tmp
env[TMPDIR] = /var/www/clients/client1/web3/tmp
env[TEMP] = /var/www/clients/client1/web3/tmp
env[PATH] = /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
Run Code Online (Sandbox Code Playgroud)
htop 的资源/过程使用情况:
问题在于您的数据库访问权限。您有多个 MySQL 进程使用 CPU,这表明数据库查询需要很长时间才能执行。
您需要查看您的应用程序,寻找以下内容:
缓慢的数据库查询会导致 PHP-FPM 耗尽处理客户端请求的可用子进程。这会导致502 Bad Gateway
错误。您可以尝试增加池的pm.max_children
设置web3
,因为这会导致错误。这可以消除可扩展性症状,但不能解决应用程序/数据库效率低下的根本原因。
如果您不使用该www
池,则可以将其删除以节省其使用的资源。
的理想设置pm.max_requests
为零,即永远不应重新启动 PHP 工作线程。如果您的 PHP 工作人员不会由于库编码错误而导致内存泄漏,那么您可以在那里使用零。否则,您可以使用使工作人员的内存使用量保持体面的任何值。关于此设置,确实没有任何其他好的建议。
在这里,您对 nginx 设置无能为力,因为 PHP-FPM 有时不可用。您可以更改gzip_comp_level
为1
,这使 nginx 花费更少的 CPU 压缩输出。但这与应用程序优化相比影响很小。
归档时间: |
|
查看次数: |
14254 次 |
最近记录: |