Nginx+;PHP-FPM+;Magento的FastCGI缓存配置
你们中的一些人可能已经注意到Magento几天前发布的最新白皮书。尽管它主要是为EE用户编写的,但我相信我们也可以使用社区版的大部分技巧 在仔细阅读之后,我继续将他们建议的Nginx+fastcgi/代理缓存配置与我的Magento标准虚拟主机配置合并,并进行了一些小的改进。以下是我的想法:Nginx+;PHP-FPM+;Magento的FastCGI缓存配置,magento,caching,nginx,fastcgi,php,Magento,Caching,Nginx,Fastcgi,Php,你们中的一些人可能已经注意到Magento几天前发布的最新白皮书。尽管它主要是为EE用户编写的,但我相信我们也可以使用社区版的大部分技巧 在仔细阅读之后,我继续将他们建议的Nginx+fastcgi/代理缓存配置与我的Magento标准虚拟主机配置合并,并进行了一些小的改进。以下是我的想法: fastcgi_cache_path /tmp/fcgi levels=1:2 keys_zone=MAGE:64m max_size=128m inactive=10h; server { listen
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以及cookies背后的实际逻辑吗?谁在实际获取缓存页面?当PHP-FPM关闭时,过时的缓存似乎不工作(?)。我有点结巴,对我得到的不同头球有些困惑
建议任何人???这种类型的配置对magento绝对没有用处,他们使用它只是为了获得最大的“虚拟”吞吐量,而且这种配置逻辑甚至在少数地方中断。
您最好配置穿孔全页缓存扩展,它将重新插入您的动态块,并使您的站点始终处于缓存中。对于新添加的产品和数量更改等,必须进行缓存刷新。我知道这是一个老问题,但如果有人偶然发现这个问题,我只想指出,最新的Magento版本(>=1.13 enterprise&>=1.8 community)将打破这种nginx缓存方法 升级并启用缓存后,如果用户正在查看缓存页面,则将无法再添加到购物车。这背后的原因是Magento在url表单键中添加了“添加到购物车”按钮,以防止跨站点脚本编写。启用nginx缓存后,将缓存第一个URL表单键,下一组用户将加载未附加到其会话的无效表单键。就我所知,也没有办法在nginx缓存中打孔,这(引用ADM的话)使得“nginx缓存完全无用”。如果有人知道是否有办法在nginx缓存中打孔,我洗耳恭听
如果您继续使用nginx缓存,我强烈建议您看看如何在不禁用它的情况下站立起来,因为在升级到最新的Magento版本时,这将为您省去许多麻烦。白皮书的直接链接:虽然此缓存配置的逻辑可能有问题,但此“配置类型”绝非无用。在某些负载配置文件下,将内容和产品页面缓存一段时间很可能会提高性能。有问题的网站可能使用库存数量管理,也可能不使用库存数量管理,所有者可能会很高兴新产品在N分钟内或手动缓存刷新后不会出现。有趣的是,因为我只在启用PHP-FPM的情况下才会遇到同样的问题。否则我可以使用nginx,没有问题,只要我启用PHP-FPM,我所有的add-to-cart链接都会产生404个错误:(Magento 1.9 CEHmm,这可能是一个完全不同的问题。我上面提到的问题在添加到购物车时没有给出404。它只会将您带到一个空的购物车页面。我们目前使用nginx+php fpm+redis运行Magento 1.13(2个实例-1个用于后端/慢速后端,1个用于完整页面缓存)。