caching404s – NGINX还是涂有漆的?

我正在寻找一个解决scheme,以在networking服务器上长期(几天/几周)caching404s。 我目前的设置是使用memcached_pa​​ss代理的NGINX和PHP-FPM提供未caching的页面(PHP也将内容写入memcached)。 networking上的爬虫似乎喜欢我的页面,每天产生几千个404请求。 所有这些都直接命中PHP,因为我无法将404响应头信息与memcached中的内容一起caching,因此memcached_pa​​ss查找总是失败。

我如何caching所有返回404的请求? Nginx的HTTPProxModule是我在找什么? 还是应该去清漆?

从我目前的观点来看,我不想改变我的整个设置,并从nginx中删除memcached_pa​​ss指令。 到目前为止,这是相当整洁的,因为php决定请求可以(应该)caching在memcached中。 必要时刷新caching也非常容易。

我目前的NGINXconfiguration文件:

server { listen 80; server_name _; gzip on; gzip_http_version 1.0; gzip_vary on; gzip_comp_level 6; gzip_proxied any; gzip_types text/plain text/html text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript; location / { gzip on; default_type "text/html; charset=utf-8"; charset utf-8; add_header Content-Encoding gzip; if ($request_method = GET) { expires max; set $memcached_key $http_host$request_uri; memcached_pass 127.0.0.1:11211; error_page 404 = @fallback; #error_page 502 = @fallback; break; } root /var/www/html/; index index.php index.html; if (!-e $request_filename) { rewrite ^/(.*)$ /index.php?q=$1 last; break; } } location @fallback { internal; root /var/www/html/; index index.php index.html; if (!-e $request_filename) { rewrite ^/(.*)$ /index.php?q=$1 last; break; } } location ~ \.php$ { root /var/www/html/; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /var/www/html/$fastcgi_script_name; include /etc/nginx/fastcgi_params; } } 

对于Nginx或Varnish来说,一个configuration的例子会很好。

谢谢! 🙂

在默认情况下清除caching404,因此不需要configuration(除了初始的基本Varnishconfiguration) – 除非后端提供了Varnish认为不可访问的回复。

如果是这种情况,可以使用VCL对答复进行必要的更改,并强制将其caching。

我没有提供任何例子,因为没有什么可以给予 – 真的。

如果你想通过nginx做到这一点,可以这样做:

 proxy_cache_valid 200 302 10m; proxy_cache_valid 404 1m; 

http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_cache_valid