6

你们中的一些人可能已经注意到最近由 Magento 发布的优化 Magento 以获得最佳性能白皮书。尽管它主要是为 EE 用户编写的,但我相信我们也可以使用社区版的大部分技巧。

仔细阅读后,我继续将他们建议的 Nginx + fastcgi/proxy 缓存配置与我的 Magento 标准虚拟主机配置合并并进行了一些小的改进。这是我想出的:

fastcgi_cache_path /tmp/fcgi levels=1:2 keys_zone=MAGE:64m max_size=128m inactive=10h;

server {
listen  99999; ## Nginx port
server_name  domain.com www.domain.com;
root  /www/magento; ## App folder
index  index.php;

location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
    expires  max;
    access_log  off;
    log_not_found  off;
}

location /index {
    try_files  $uri @fcgi_nocache;
}

location /checkout {
    try_files  $uri @fcgi_nocache;
}

location / {
    try_files  $uri @fcgi_cache;
    if ($cookie_frontend) { return 413; }
    if ($cookie_CUSTOMER_AUTH) { return 413; }
    if ($request_method = POST ) { return 413; }
    error_page 413 = @fcgi_nocache;
}

# Deny access to hidden files
location ~ (/(app/|includes/|/pkginfo/|var/|report/config.xml)|/\.svn/|/.hta.+) {
    deny  all;
}

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

# Manually purge pages
location ~ /purge(/.*) {
    fastcgi_cache_purge MAGE "$scheme$request_method$host$1";
}

location @fcgi_cache {
    #if (!-e $request_filename) { rewrite / /index.php last; } ## Catch 404s that try_files miss
    fastcgi_pass  unix:/var/spool/phpfpm.sock; ## php-fpm socket
    include  fastcgi_params;
    fastcgi_connect_timeout  60;
    fastcgi_send_timeout  60;
    fastcgi_read_timeout  60;
    fastcgi_buffer_size  4k;
    fastcgi_buffers  512 4k;
    fastcgi_busy_buffers_size  8k;
    fastcgi_temp_file_write_size  256k;
    fastcgi_intercept_errors  off;        
    fastcgi_param SCRIPT_FILENAME  $document_root/index.php;
    fastcgi_param SCRIPT_NAME  /index.php;
    #fastcgi_keep_conn  on; # NGINX 1.1.14        
    fastcgi_temp_path  /tmp/fcgi2 1 2;

    fastcgi_cache  MAGE;
    #fastcgi_cache_key  "$request_method|$http_if_modified_since|$http_if_none_match|$host|$request_uri"; ## Original
    fastcgi_cache_key  "$scheme$request_method$host$request_uri$http_if_modified_since$http_if_none_match";
    #fastcgi_cache_lock  on 5s; # NGINX 1.1.12
    fastcgi_cache_valid  200 301 302 304 1h;
    fastcgi_hide_header  "Set-Cookie";

    if ($http_cookie !~ "X-Store=1" ) {
        add_header Set-Cookie "X-Store=1; path=/";
    }

    fastcgi_ignore_headers  "Cache-Control" "Expires" "Set-Cookie";
    fastcgi_cache_min_uses  1;
    fastcgi_cache_valid  30m;
    fastcgi_cache_use_stale  updating error timeout invalid_header http_500;
    fastcgi_cache_bypass  $cookie_EXTERNAL_NO_CACHE $cookie_CUSTOMER_AUTH;
    fastcgi_no_cache  $cookie_EXTERNAL_NO_CACHE $cookie_CUSTOMER_AUTH;

    #add_header  X-Cache-Status $upstream_cache_status; # Test
}

location @fcgi_nocache {
    #if (!-e $request_filename) { rewrite / /index.php last; } ## Catch 404s that try_files miss
    fastcgi_pass  unix:/var/spool/phpfpm.sock; ## php-fpm socket
    include  fastcgi_params;
    fastcgi_connect_timeout  60;
    fastcgi_send_timeout  60;
    fastcgi_read_timeout  60;
    fastcgi_buffer_size  4k;
    fastcgi_buffers  512 4k;
    fastcgi_busy_buffers_size  8k;
    fastcgi_temp_file_write_size  256k;
    fastcgi_intercept_errors  off;        
    fastcgi_param SCRIPT_FILENAME  $document_root/index.php;
    fastcgi_param SCRIPT_NAME  /index.php;
    #fastcgi_keep_conn  on; # NGINX 1.1.14        
    fastcgi_temp_path  /tmp/fcgi2 1 2;

    if ($http_cookie !~ "X-Store=1" ) {
        add_header Set-Cookie "X-Store=1; path=/";
    }
    #add_header  X-Cache-Status $upstream_cache_status; # Test
}

}

经过一些测试,AB 的结果似乎令人印象深刻,但我真的不相信它们是否准确以及缓存系统是否完全按预期工作。有人可以详细说明 @fcgi_cache 和 @fcgi_nocache 以及 cookie 背后的实际逻辑是什么吗?谁实际上在获取缓存页面?当 PHP-FPM 关闭时,陈旧的缓存似乎不起作用(?)。我对我得到的不同标题有点困惑和困惑。

有人建议吗??

4

2 回答 2

2

this type of config is absolutely useless for magento, they used it only to get maximum "dummy" throughput, and this config logics even breaks in few places. you would better configure hole-punching full page cache extension, it will reinsert your dynamic blocks and will keep your site always in cache. there must be as well cache refresh for newly added products and qty changes, etc.

于 2012-11-14T11:50:59.240 回答
0

我知道这是一个老问题,但如果有人偶然发现这个线程,我只想指出最新的 Magento 版本(>=1.13 企业和>=1.8 社区)将破坏这种 nginx 缓存方法。

升级并启用缓存后,如果您的用户正在查看缓存页面,他们将无法再添加到购物车。这背后的原因是,Magento 为“添加到购物车”按钮添加了一个 url 表单键,以防止跨站点脚本。打开 nginx 缓存后,第一个 URL 表单键将被缓存,下一组用户将加载未附加到其会话的无效表单键。据我所知,也没有办法打孔 nginx 缓存,这(引用 ADM)使得“nginx 缓存绝对无用”。如果有人知道是否有办法打孔 nginx 缓存,我会全神贯注。

如果您继续使用 nginx 缓存,我强烈建议您看看如何在不禁用它的情况下站起来,这将在升级到最新的 Magento 版本时为您省去很多麻烦。

于 2014-06-03T15:09:27.460 回答