使用nginx和redis缓存404响应

使用nginx和redis缓存404响应,nginx,Nginx,我目前正在使用NGINX和Redis来缓存它的响应。现在,我想缓存404响应。我的配置如下: location / { set $redis_key "responsecache:$device:$is_ajax:$http_host$request_uri"; redis_pass redis.betalabs.net:6379; add_header X-Cache "Hit"; default_typ

我目前正在使用NGINX和Redis来缓存它的响应。现在,我想缓存404响应。我的配置如下:

location / {
    set $redis_key "responsecache:$device:$is_ajax:$http_host$request_uri";

    redis_pass     redis.betalabs.net:6379;
    add_header     X-Cache "Hit";
    default_type   text/html;
    charset        UTF-8;
    error_page     401 404 405 = @fallback;
    error_page     502 = @fallback;
}

location @fallback {
    try_files $uri $uri/ /index.php?$query_string;
}

location = /favicon.ico { return 404; }

access_log off;
server_tokens off;
error_log  /var/log/nginx/error.log error;

sendfile off;

client_max_body_size 100m;

if ($request_uri ~* "^(.*/)index\.php$") {
    return 301 $1;
}

location ~ \.php$ {
    try_files = $uri $uri/ /index.php?$query_string;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param PATH_INFO $fastcgi_path_info;
}

location ~ /\.ht {
    deny all;
}
我怀疑此行避免了要缓存的404个错误:

error_第401404405页=@fallback

我试着这样改变它:

location / {
    set $redis_key "responsecache:$device:$is_ajax:$http_host$request_uri";

    redis_pass     redis.betalabs.net:6379;
    add_header     X-Cache "Hit";
    default_type   text/html;
    charset        UTF-8;
    error_page     401 404 405 = @fallback;
    error_page     502 = @fallback;
}

location @fallback {
    try_files $uri $uri/ /index.php?$query_string;
}

location = /favicon.ico { return 404; }

access_log off;
server_tokens off;
error_log  /var/log/nginx/error.log error;

sendfile off;

client_max_body_size 100m;

if ($request_uri ~* "^(.*/)index\.php$") {
    return 301 $1;
}

location ~ \.php$ {
    try_files = $uri $uri/ /index.php?$query_string;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param PATH_INFO $fastcgi_path_info;
}

location ~ /\.ht {
    deny all;
}
错误\u第401 405页=@回退

但是没有运气。我很难在网上找到解决方案

有人知道怎么做吗