bnf/nginx-cache

TYPO3 的 NGINX 缓存管理器

安装: 853

依赖项: 0

建议者: 0

安全性: 0

星标: 23

关注者: 3

分支: 3

开放问题: 3

类型:typo3-cms-extension

3.0.1 2024-01-30 15:34 UTC

README

此 TYPO3 扩展为使用 NGINX 的 fastcgi_cache 功能添加了必要的组件。它添加了适当的缓存控制头,记录了所需的 NGINX 配置,并在内容更改时刷新 nginx 缓存。

配置

只需安装扩展和所需的 nginx 模块,无需在 TYPO3 中进行配置。

vendor/bin/typo3 extension:activate nginx_cache

# Fedora (RPM)
sudo dnf install nginx nginx-mod-http-perl perl-Digest-MD5 perl-File-Find

# Debian (dpkg)
sudo apt install nginx-extras libnginx-mod-http-perl libdigest-md5-file-perl

# Fedora <=32 (RPM)
sudo dnf install nginx nginx-mod-http-perl perl-Digest-MD5

现在您需要配置 NGINX。

NGINX 配置

您需要配置 NGINX 以缓存 php fastcgi 调用的输出。因此,您需要在 http {} 部分(它位于您的 server {} 部分之外)指定一个 fastcgi 缓存路径

http {
    fastcgi_cache_path /var/nginx/cache/TYPO3 levels=1:2 keys_zone=TYPO3:10m inactive=24h;

    # We suggest to copy the path to /etc/nginx/perl/lib, to prevent attackers (that use
    # TYPO3 security issues) to inject a new purge.pm, and thus get access to e.g.
    # your https keys (That means on EXT:nginx_cache major update you need to check
    # for changes. Promise: We'll do that for major upgrades only).
    # So use:
    #perl_modules /etc/nginx/perl/lib;
    # ..instead of:
    perl_modules /path/to/your/webroot/typo3conf/ext/nginx_cache/Resources/Private/nginx_purge;

    perl_require purge.pm;
}

在您的 php 位置块中,您需要配置所需的缓存行为

error_page 405 = @purge;
if ($request_method = PURGE) {
    return 405;
}

location ~ \.php$ {
    include fastcgi.conf;
    fastcgi_pass php-fpm;

    fastcgi_cache TYPO3;
    # Use the current request url as cache key
    fastcgi_cache_key $scheme://$host$request_uri;
    # Only cache GET/HEAD requests (not POST or PUT)
    fastcgi_cache_methods GET HEAD;
    # Do not deliver cached content for logged in be users, or requests with a query string
    fastcgi_cache_bypass $cookie_be_typo_user $args;
    # Consider every request non-cacheable – cachability is explicitly defined through TYPO3
    # (using X-Accel-Expires header)
    fastcgi_cache_valid 0;
    # Ignore all cache headers, besides X-Accel-Expires
    fastcgi_ignore_headers "Expires" "Cache-Control" "Vary";
}

# For debugging only
add_header X-Cache $upstream_cache_status;

location @purge {
    allow 127.0.0.1;
    allow ::1;
    # Depending on your servers setup (e.g. if your server is NATed to the public ip, or your fastcgi
    # server is running on another ip) you may also need to define the allowed purge source ip's here
    # If the cache flush (when clearing the caches in TYPO3) fails with the setting "allow 127.0.0.1",
    # you can find the reasons in the nginx error log. Open a shell and execute:
    #   tail -f /var/log/nginx/error.log"
    # ..and perform a frontend cache flush. You should see errors like:
    #   access forbidden by rule, client: YY.YYY.YYY.YY, server: www.example.com, request: "PURGE / HTTP/1.1"
    # In that case add the printed ip to the list of allowed source addresses:
    #allow YY.YYY.YYY.YY;
    deny all;

    set $purge_path "/var/nginx/cache/TYPO3";
    set $purge_levels "1:2";
    set $purge_cache_key "$scheme://$host$request_uri";
    set $purge_all 0;
    if ($request_uri = /*) {
        set $purge_all 1;
    }

    perl NginxCache::Purge::handler;
}

location / {
    try_files $uri $uri/ /index.php$is_args$args;
}

请确保已正确设置时区,否则缓存可能过晚失效。(尽管这也适用于 TYPO3 核心)。/etc/php.ini

date.timezone = "Europe/Berlin"

在 CLI 环境中清除缓存

目前,扩展不支持在 CLI 环境中清除缓存。如果您想清除 nginx fastcgi 缓存(例如,作为部署过程的一部分),可以执行以下命令

curl -X PURGE https://www.domain.tld/*

相对于 nc_staticfilecache 的优势

  • 可以缓存头信息(config.additionalHeaders)
  • 验收测试
  • 对开始时间/结束时间的性能支持(只要 TYPO3 能够正确计算缓存时间)(公平地说:nc_staticfilecache 通过自动生成的 .htaccess 文件提供此功能,但仅适用于 apache,不适用于 nginx)

版本