识别瓶颈 – Nginx + PHP-FPM + Ubuntu

我有一个运行PHP-FPM和Nginx的Ubuntu服务器安装程序 – 我做了一些压力testing,并经历了一个大幅度的减速。 我用250用户loadImpact。 我发现,在testing期间,PHP并没有放慢速度 – 页面渲染时间没有从0.04秒(我认为这是因为APCcaching)而动摇。

这些资产的实际传输是如此之久。 不知道这是一个networking的限制,还是一个Nginx的问题 – 我假设它是Nginx的B / C服务器是在Rackspace的云服务器,我认为他们的networking是非常强大的(也许这是一个愚蠢的假设… )。

从命令行运行“顶部”显示一个nginx进程在任何给定的时间运行,我认为这是瓶颈 – 而且,CPU几乎没有被使用。 值得一提的是我使用的是512MB RAM云服务器,但是RAM的使用率也很低,所以我很确定我没有configuration好Nginx。 我在下面粘贴了我的conf …

我对此很新,所以如果我没有提供足够的信息,请提前道歉。

user www-data; worker_processes 4; #using a quad-core VPS server error_log /var/log/nginx/error.log; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/mime.types; default_type application/octet-stream; access_log /var/log/nginx/access.log; sendfile on; tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 3; tcp_nodelay on; gzip on; gzip_comp_level 1; gzip_proxied any; gzip_types text/plain text/html text/css application/x-javascript text/xml application/x$ include /etc/nginx/conf.d/*.conf; include /etc/nginx/sites-enabled/*; server { listen *:80; server_name ***.com; location / { root /var/www/nginx-default; index index.php; auth_basic "Restricted"; auth_basic_user_file /etc/nginx/htpass; } location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /var/www/nginx-default$fastcgi_script_na$ include fastcgi_params; } } 

这是虚拟主机configuration:

 server { listen 80; server_name dev.***.com; access_log /***/access.log; error_log /***/error.log; client_max_body_size 4M; location / { root /***; index index.php; # if file exists return it right away if (-f $request_filename) { break; } # otherwise rewrite the fucker if (!-e $request_filename) { rewrite ^/(.+)$ /index.php?url=$1 last; break; } location ~ \.php$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /***$fastcgi_script_name; include fastcgi_params; } } } 

在此先感谢您的任何build议!

你如何到达0.04s渲染时间? 您可能希望从等式中完全移除PHP,而不是仅针对静态文件。

不过,有几件事可以改善你的configuration。 首先,你的gzip压缩级别可以相当安全的增加,CPU的影响是最小的,但是你可以通过增加到5来获得更小的数据大小。我发现进一步增加它比很less得到任何好处。

你也可以很容易地增加你的保活超时时间,默认值是75.这可能不会对原始吞吐量产生太大的帮助,但是如果客户端的浏览器允许,那么它看起来会更快。

之后,您可能需要使用打开文件caching。 我使用以下configuration:

 open_file_cache max=5000 inactive=20s; open_file_cache_valid 30s; open_file_cache_min_uses 2; open_file_cache_errors on; 

你可以在这里find相关的文档: http : //wiki.nginx.org/NginxHttpCoreModule#open_file_cache

由于没有指定缓冲区,因此缓冲区可能不够大,因此响应暂时存储在磁盘上。 你可以看到,如果iostat 1显示一个大的艾奥瓦时间。

有趣的缓冲区是

  • client_body_buffer_size
  • output_buffer

客户端正文缓冲区logging在这里: http : //wiki.nginx.org/NginxHttpCoreModule#client_body_buffer_size

输出缓冲区没有logging在wiki中,但是它有两个参数,第一个是缓冲区的数量,第二个是它们的大小。 输出缓冲区主要在sendfileclosures时使用,或者在将数据发送给用户之前先将数据gzip使用,所以您一定要确保数据符合要求。