我有一些静态内容与高速caching控制Max-Age头连接到它,以便客户端将caching静态内容。 但是,当存在错误响应,build议客户端将其caching时,IIS 7.5仍会将此标头发送出去。
有负面影响,一些代理将caching该错误响应。 我可以Vary: Accept,Accept-Encoding但这并没有真正解决Max-Age错误响应的根本问题。
当前相关的IIS web.config部分是:
<configuration> <system.webServer> <staticContent> <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="7.00:00:00" /> </staticContent> </system.webServer> </configuration>
有没有办法让我可以做到这一点,所以我们不告诉客户端或代理caching400/500错误代码?
我创build了一个基本的testing“套件”。
当我在IIS 7.0(.NET 4.0上的集成pipe道模式)上运行最小化Web.config的testing时,一切都通过了; 当请求的Accept头与文件的Content-Type不匹配时,testing文件的Cache-Control响应头设置为private 。
这导致我相信你有一些模块中断IIS的静态caching例程或IIS 7.0和7.5不同。
这里是我使用的文件(sans some-script.js因为它只是一个空文件):
Web.Config:
<?xml version="1.0"?> <configuration> <system.web> <compilation debug="true" targetFramework="4.0"> </compilation> </system.web> <system.webServer> <staticContent> <!-- Set expire headers to 30 days for static content--> <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="7.00:00:00" /> </staticContent> </system.webServer> </configuration>
test.html:
<!doctype html> <html> <head> <title>http://serverfault.com/questions/346975</title> <style> body > div { border:1px solid; padding:10px; margin:10px; } </style> </head> <body> <div> <h2>Request JS file with Accepts: accept/nothing</h2> <b>Response Headers: </b> <pre id="responseHeaders-1">loading&hellip</pre> </div> <div> <h2>Request JS file with Accepts: */*</h2> <b>Response Headers: </b> <pre id="responseHeaders-2">loading&hellip</pre> </div> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script> <script> var responseHeaders1 = $("#responseHeaders-1"), responseHeaders2 = $("#responseHeaders-2"), fetchScript = function (accepts, element, successMsg, errorMsg) { var jXhr = $.ajax({ // fetch the resource "fresh" each time since we are testing the Cache-Control header and not caching itself "url": "some-script.js?" + (new Date).getTime(), "headers": { "Accept" : accepts }, "complete": function () { var headers = jXhr.getAllResponseHeaders(); headers = headers.replace(/(Cache-Control:.+)/i, "<strong><u>$1</u></strong>"); element.html(headers); }, "success": function () { element.after("<div>" + successMsg + "</div>"); }, "error": function () { element.after("<div>" + errorMsg + "</div>"); } }); }; fetchScript("accept/nothing", responseHeaders1, "Uh, your server is sending stuff when the client doesn't accept it.", "Your server (probably) responded correctly."); fetchScript("*/*", responseHeaders2, "Your server responded correctly.", "Something went wrong."); </script> </body> </html>
您应该指定要caching的内容types。 例如,你可以caching脚本,CSS,图像..等等。 所以在<system.webServer>标签之前使用<location path ="Scripts"> <system.webServer>标签。 所以你的webconfiguration看起来像这样。
<location path ="Scripts"> <system.webServer> <staticContent> <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="07:00:00" /> </staticContent> </system.webServer> </location> <location path ="css"> <system.webServer> <staticContent> <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="07:00:00" /> </staticContent> </system.webServer> </location>