Magento + Nginx 从时间到第一个字节的等待时间过长

Magento + Nginx 从时间到第一个字节的等待时间过长

我遇到了 TTFB 时间过长的问题,尤其是发送后的等待时间。

Webpagetest 显示 TTFB 大约为 4 秒。

Pingtom 指定等待时间是此处的瓶颈

以下是我的 Magento + Nginx 服务器设置,请参阅测试和配置的屏幕截图http://share.pho.to/#7BuP9

  1. 操作系统:Ubuntu 14.04 服务器
  2. nginx/1.4.6
  3. PHP 5.5.9
  4. opcache 7.0.3
  5. memcached
  6. PHP-FPM
  7. Magento 1.9.0.1
  8. Linode 2GB /2Core 服务器

Nginx 服务器配置

server {
    server_name dev.magibooks.com;
    return 301 $scheme://www.dev.magibooks.com$request_uri;
}


server {
listen 80;

#listen 443 ssl spdy;
#ssl on;
#ssl_certificate      /etc/nginx/ssl/dev/ssl-dev-unified.crt;
#ssl_certificate_key  /etc/nginx/ssl/dev/ssl-dev.key;

server_name www.dev.magibooks.com;
root /var/www/www.dev.magibooks.com/web;

## rewrites dev.magibooks.com to www.dev.magibooks.com
#if ($http_host != "www.dev.magibooks.com") {
#    rewrite ^ $scheme://www.dev.magibooks.com$request_uri permanent;
#}

location / {
    index index.html index.php; ## Allow a static html file to be shown first
    try_files $uri $uri/ @handler; ## If missing pass the URI to Magento's front handler
    expires 30d; ## Assume all files are cachable

    #auth_basic           "Restricted"; ## Message shown in login window
    #auth_basic_user_file /var/www/www.dev.magibooks.com/.htpasswd; 
    #autoindex            on;



}

location ~ /\.(eot|otf|ttf|woff)$ {
    add_header Access-Control-Allow-Origin *;
}

## These locations would be hidden by .htaccess normally
location /app/                { deny all; }
location /includes/           { deny all; }
location /lib/                { deny all; }
location /media/downloadable/ { deny all; }
location /pkginfo/            { deny all; }
location /report/config.xml   { deny all; }
location /var/                { deny all; }

location /var/export/ { ## Allow admins only to view export folder
    auth_basic           "Restricted"; ## Message shown in login window
    auth_basic_user_file /var/www/www.dev.magibooks.com/.htpasswd; ## See /etc/nginx/htpassword
    autoindex            on;
}

## Disable .htaccess and other hidden files
location ~ /\. {
    deny all;
    access_log off;
    log_not_found off;
}

location @handler { ## Magento uses a common front handler
    rewrite / /index.php;
}

location ~ \.php/ { ## Forward paths like /js/index.php/x.js to relevant handler
    rewrite ^(.*\.php)/ $1 last;
}

location ~ \.php$ { ## Execute PHP scripts
    if (!-e $request_filename) { rewrite / /index.php last; } ## Catch 404s that try_files miss

    expires        off; ## Do not cache dynamic content
    #fastcgi_pass   127.0.0.1:9000;
    fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_param  HTTPS $fastcgi_https;
    fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
    #fastcgi_param  MAGE_RUN_CODE default; ## Store code is defined in administration >     Configuration > Manage Stores
    fastcgi_param  MAGE_RUN_TYPE store;
    include        fastcgi_params; ## See /etc/nginx/fastcgi_params
}


location ~* .(jpg|jpeg|png|gif|ico|css|js)$ {
    expires 365d;
}
}

nginx.conf

user www-data;
worker_processes 2;
pid /run/nginx.pid;
worker_rlimit_nofile 60000;

events {
    worker_connections 10240;
    # multi_accept on;
}

http {

    ##
    # Basic Settings
    ##

    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 2;
    types_hash_max_size 2048;
    # server_tokens off;

    # server_names_hash_bucket_size 64;
    # server_name_in_redirect off;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ##
    # Logging Settings
    ##

    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##

    gzip on;
    gzip_disable "msie6";

    #gzip_vary on;
    gzip_proxied any;
    gzip_comp_level 6;
    gzip_min_length 1100;
    gzip_buffers 16 8k;
    gzip_http_version 1.1;
    gzip_types text/plain application/xml text/css text/js text/xml application/x-javascript text/javascript application/json application/xml+rss;

    ##
    # nginx-naxsi config
    ##
    # Uncomment it if you installed nginx-naxsi
    ##

    #include /etc/nginx/naxsi_core.rules;

    ##
    # nginx-passenger config
    ##
    # Uncomment it if you installed nginx-passenger
    ##

    #passenger_root /usr;
    #passenger_ruby /usr/bin/ruby;

    ##
    # Virtual Host Configs
    ##

    ## Detect when HTTPS is used
    map $scheme $fastcgi_https {
      default off;
      https on;
    }

    client_body_buffer_size 10K;
    client_header_buffer_size 1k;
    client_max_body_size 8m;
    large_client_header_buffers 4 4k;


    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}

我尝试过以下方法

  1. +/- Cloudflare
  2. 优化 php-fpm pm_* 值
  3. 优化缓存,包括 opcache 和 memcached,这些更改加快了整页和文档加载完成时间,但对 TTFB 时间没有影响
  4. +/- nginx 日志
  5. 调整 gzip 级别从 1 到 6
  6. CMS 页面加载速度稍快一些,但 TTFB 仍为 1.815 秒
  7. phpinfo() 页面加载大约需要 40ms
  8. 通过跟踪 access.log,请求总是在准确的等待时间后显示,例如,当我刷新 dev.magibooks.com 时,服务器端需要 4 秒钟才能看到请求出现在 access.log 上
  9. 我怀疑这与主页上的产品太多有关,尤其是可配置产品,我删除了 12 个产品,TTFB 从 5 秒减少到 4 秒,但仍然明显偏高
  10. php 慢页面显示以下内容

    [17-Sep-2014 21:48:45]  [pool www] pid 10333
    script_filename = /var/www/www.dev.magibooks.com/web/index.php
    [0x00007fc13eb6f8d8] session_start() /var/www/www.dev.magibooks.com/web/app/code/core/Mage/Core/Model/Session/Abstract/Varien.php:125
    [0x00007fc13eb6f798] start() /var/www/www.dev.magibooks.com/web/app/code/core/Mage/Core/Model/Session/Abstract/Varien.php:168
    [0x00007fc13eb6f650] init() /var/www/www.dev.magibooks.com/web/app/code/core/Mage/Core/Model/Session/Abstract.php:84
    [0x00007fc13eb6f558] init() /var/www/www.dev.magibooks.com/web/app/code/core/Mage/Core/Model/Session.php:42
    [0x00007fc13eb6f428] __construct() /var/www/www.dev.magibooks.com/web/app/code/core/Mage/Core/Model/Config.php:1348
    [0x00007fc13eb6f310] getModelInstance() /var/www/www.dev.magibooks.com/web/app/Mage.php:463
    [0x00007fc13eb6f1e0] getModel() /var/www/www.dev.magibooks.com/web/app/Mage.php:477
    [0x00007fc13eb6f088] getSingleton() /var/www/www.dev.magibooks.com/web/app/code/core/Mage/Core/Controller/Varien/Action.php:493
    [0x00007fc13eb6ef00] preDispatch() /var/www/www.dev.magibooks.com/web/app/code/core/Mage/Adminhtml/Controller/Action.php:160
    [0x00007fc13eb6ed08] preDispatch() /var/www/www.dev.magibooks.com/web/app/code/core/Mage/Core/Controller/Varien/Action.php:407
    [0x00007fc13eb6eaf8] dispatch() /var/www/www.dev.magibooks.com/web/app/code/core/Mage/Core/Controller/Varien/Router/Standard.php:250
    [0x00007fc13eb6e9a8] match() /var/www/www.dev.magibooks.com/web/app/code/core/Mage/Core/Controller/Varien/Front.php:172
    [0x00007fc13eb6e850] dispatch() /var/www/www.dev.magibooks.com/web/app/code/core/Mage/Core/Model/App.php:354
    [0x00007fc13eb6e6b0] run() /var/www/www.dev.magibooks.com/web/app/Mage.php:684
    [0x00007fc13eb6e570] run() /var/www/www.dev.magibooks.com/web/index.php:87 11. minified all js and css
    

答案1

我已采取以下措施,成功将页面加载时间从 6 秒缩短至 1 秒:

  1. 禁用 newrelic
  2. 禁用 longview

待办事项 1. 我正在升级到 4 核 4GB 内存系统,我将在这里发布结果 2. 我将咨询全页缓存解决方案。

答案2

Magento 的标准速度非常快,关闭所有缓存的情况下,它将在 0.5 秒内加载页面,而打开所有缓存的情况下,它将在 0.2 秒内加载页面。

因此,您不需要严格地让 Magento 运行得更快,您只需删除/更改使其变慢的任何因素。

存在两个性能瓶颈。

  • 糟糕的托管
  • 糟糕的代码

解决这些问题非常简单,

  • 使用专门提供高性能 Magento 托管的托管服务提供商,该提供商可以提供商店级支持,以帮助您扩展并指导您改进性能
  • 分析你的代码,并删除/重构性能不佳的代码

只需安装一个包含示例数据的演示商店,您就会知道托管提供商是否是瓶颈。如果在关闭所有缓存的情况下加载时间超过 0.5 秒,那么问题就出在这里。

相关内容