nginx + php-fastcgi 运行缓慢

nginx + php-fastcgi 运行缓慢

php-fastcgi 运行缓慢,出现问题。一直出现 504 超时。有什么建议吗?以下是 nginx 站点配置、顶部输出和错误日志:

server {

        listen 80;
        server_name  example.com

location / {
                root   /var/www/example/htdocs;
                index  index.html index.htm index.php;

                # Joomla SEF section
                if ( !-e $request_filename ) {
                        rewrite (/|\.php|\.html|\.htm|\.feed|\.pdf|\.raw|/[^.]*)$ /index.php last;
                        break;
                }

        }

location ~ \.php$ {
                fastcgi_pass   127.0.0.1:9002;
                fastcgi_index  index.php;
                fastcgi_param  SCRIPT_FILENAME  /var/www/example/htdocs$fastcgi_script_name;
                include /etc/nginx/fastcgi_params;
        }

location ~* ^.+\.(js|swf|css|jpg|jpeg|gif|png)$ {
                root /var/www/example/htdocs;
                expires 14d;
        }
}

当服务器运行缓慢时,top 命令如下:

top - 22:52:10 up 207 days, 21:24,  1 user,  load average: 2.25, 2.07, 2.05
Tasks: 126 total,   2 running, 124 sleeping,   0 stopped,   0 zombie
Cpu(s): 13.3%us, 11.2%sy,  0.0%ni, 49.6%id,  0.2%wa,  0.0%hi,  0.0%si, 25.8%st
Mem:   1639756k total,  1444380k used,   195376k free,   236768k buffers
Swap:  3145720k total,    18908k used,  3126812k free,   642228k cached

 PID USER      PR  NI  VIRT  RES  SHR S %CPU %MEM    TIME+  COMMAND                                                                                             
 702 mysql     20   0  350m  35m 5344 S   69  2.2  38:36.62 mysqld                                                                                               
 6659 www-data  20   0  197m  20m 3764 S   11  1.3   1:14.59 php-cgi                                                                                              
 6658 www-data  20   0  197m  19m 3776 S    7  1.2   1:06.68 php-cgi                                                                                              
 6660 www-data  20   0  199m  20m 4380 S    6  1.3   1:11.56 php-cgi                                                                                              
 6662 www-data  20   0  191m  14m 3732 S    4  0.9   1:08.79 php-cgi                                                                                              
 6661 www-data  20   0  197m  20m 3744 S    3  1.3   1:14.09 php-cgi

nginx 日志显示了很多这些错误:

2011/01/25 09:06:37 [error] 13031#0: *850 upstream timed out (110: Connection timed out) while connecting to upstream, client: 50.8.71.165, server: example.com, request: "GET /events/item/root/calendar_event HTTP/1.1", upstream: "fastcgi://127.0.0.1:9001", host: "example.com", referrer: "http://www.something.com/calendar.cfm?fuseaction=ViewEventDetails&EventID=304"

答案1

问题肯定与 MySQL 进程的 CPU 使用率约为 70% 有关。您在 PHP/Nginx 中收到错误的原因是,如果与 MySQL 的连接时间过长,PHP 可能会抛出错误。

一个建议是针对这种使用情况调整 MySQL 性能。一个很好的诊断工具是mk-查询-摘要,这可以帮助查找MySQL中的瓶颈。

另一个(更简单的)解决方案是升级数据库服务器(或将其卸载到单独的服务器),这样 MySQL 就有更多内存/磁盘资源可供使用。

相关内容