我正在用 nginx 做简单的缓存。它适用于 200 个响应,但 404 个响应由于某种原因没有“命中”缓存逻辑,我不明白为什么。
我所说的“没有命中缓存逻辑”是基于我的add_header X-Cached
. 如果是 404 响应,则根本不会出现此标头。
404 响应:
< HTTP/1.1 404 Not Found
< Server: nginx
< Date: Mon, 05 Dec 2016 09:54:34 GMT
< Content-Type: application/json
< Content-Length: 55
< Connection: keep-alive
< Expires: Thu, 19 Nov 1981 08:52:00 GMT
< Cache-Control: no-store, no-cache, must-revalidate
< Pragma: no-cache
200 回复:
< HTTP/1.1 200 OK
< Server: nginx
< Date: Mon, 05 Dec 2016 09:54:56 GMT
< Content-Type: application/json
< Content-Length: 1186
< Connection: keep-alive
< Set-Cookie: PHPSESSID=5hq364dphuo8ka26sbcadiak74; path=/
< Expires: Thu, 19 Nov 1981 08:52:00 GMT
< Cache-Control: no-store, no-cache, must-revalidate
< Pragma: no-cache
< X-Cached: MISS
相关的nginx配置:
location / {
try_files $uri /index.php$is_args$args;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_cache_key $request_uri?$is_args$args;
fastcgi_cache CACHENAME;
fastcgi_cache_valid 200 90d;
fastcgi_cache_valid 404 365d;
fastcgi_cache_use_stale updating error timeout invalid_header http_404 http_500;
fastcgi_cache_methods GET HEAD;
fastcgi_ignore_headers Cache-Control Set-Cookie Expires X-Accel-Expires Vary;
add_header X-Cached $upstream_cache_status;
}
我的应用程序是一个 REST API,所以 404 的响应只是说路由返回了错误的响应。我希望缓存此响应。
我究竟做错了什么?感谢您的任何想法!