iis可以缓存前端文件,减少第2次访问网站对静态文件的请求,诸如:.css .js .woff等文件,但要生效必须有相应规则,同时还要在配置编辑器里面设置这些,它才会生效,否则也只是看看罢了,它是不会有任何效果的。
下面就介绍下具体的操作方法。
//web.config 前端缓存设置

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | <system.webServer> <!-- step1: launch your iis manager and go to the compression module. if it's not installed, go to the server manager, roles, web server. under role services, check your installed roles. if dynamic compression isn't installed, click add roles and install it. you can go back to compression for your site and ensure dynamic compression is checked. at this point, dynamic compression should be setup, but you really need to be specific about what mimeTypes will be compressed. back in iis manager, go to the page for the server, not the site. click on configuration editor: from the dropdown, select system.webServer/httpCompression: then click on dynamic types and now that you're in the list editor,think about what types you want compressed. by default */* is false, but you could just turn that on. i chose to be a little more picky and added application/atom+xml, application/json, and application/atom+xml;charset=utf-8 as seen below. it's a little gotcha that application/atom+xml and application/atom+xml;charset=utf-8 are separate entries.feel free to add what ever mimeTypes you like in here. after you've added them and closed the dialog, be sure to click apply and Restart your iis service to load the new module. step2: we need to set a mimetype for javascrip there, so configure some other types too. Notice minFileSizeForComp, this specifies the minimum number of kilobytes a file must contain in order to use on-demand compression. or command lines rule! if you find all this clicking and screenshots offensive, then do it all from the command line using appcmd for iis7.that's lovely also. //gzip json atom+xml atom+xml;charset=utf-8 %windir%/system32/inetsrv/appcmd.exe set config -section:system.webServer/httpCompression /+"dynamicTypes.[mimeType='application/json',enabled='True']" /commit:apphost %windir%/system32/inetsrv/appcmd.exe set config -section:system.webServer/httpCompression /+"dynamicTypes.[mimeType='application/atom%u002bxml',enabled='True']" /commit:apphost %windir%/system32/inetsrv/appcmd.exe set config -section:system.webServer/httpCompression /+"dynamicTypes.[mimeType='application/atom%u002bxml;charset=utf-8',enabled='True']" /commit:apphost --> <!-- BEGIN httpCompression --> <httpCompression minFileSizeForComp="0"> <scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll" staticCompressionLevel="7" /> <dynamicTypes> <clear /> <add mimeType="text/*" enabled="true" /> <add mimeType="message/*" enabled="true" /> <add mimeType="application/x-javascript" enabled="true" /> <add mimeType="*/*" enabled="false" /> <add mimeType="image/svg+xml" enabled="true" /> <add mimeType="application/font-woff" enabled="true" /> <add mimeType="application/x-font-ttf" enabled="true" /> <add mimeType="application/octet-stream" enabled="true" /> <add mimetype="application/json" enabled="true" /> <add mimetype="application/atom+xml" enabled="true" /> <add mimetype="application/atom+xml;charset=utf-8" enabled="true" /> </dynamicTypes> <staticTypes> <clear /> <add mimeType="text/*" enabled="true" /> <add mimeType="message/*" enabled="true" /> <add mimeType="application/x-javascript" enabled="true" /> <add mimeType="application/atom+xml" enabled="true" /> <add mimeType="application/xaml+xml" enabled="true" /> <add mimeType="*/*" enabled="false" /> <add mimeType="image/svg+xml" enabled="true" /> <add mimeType="application/font-woff" enabled="true" /> <add mimeType="application/font-woff2" enabled="true" /> <add mimeType="application/x-font-ttf" enabled="true" /> <add mimeType="application/octet-stream" enabled="true" /> <add mimetype="application/json" enabled="true" /> <add mimetype="application/atom+xml" enabled="true" /> <add mimetype="application/atom+xml;charset=utf-8" enabled="true" /> </staticTypes> </httpCompression> <!-- END httpCompression --> <!-- browser cache (or client cache), and mimeMappings for iis --> <!-- BEGIN browser cache and mimemappings --> <staticContent> <clientCache cacheControlCustom="public" cacheControlMode="UseMaxAge" cacheControlMaxAge="30.00:00:00" /> <!-- <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="30.00:00:00" /> --> <remove fileExtension=".htm" /> <mimeMap fileExtension=".htm" mimeType="text/html;charset=UTF-8" /> <remove fileExtension=".html" /> <mimeMap fileExtension=".html" mimeType="text/html;charset=UTF-8" /> <remove fileExtension=".shtm" /> <mimeMap fileExtension=".shtm" mimeType="text/html;charset=UTF-8" /> <remove fileExtension=".shtml" /> <mimeMap fileExtension=".shtml" mimeType="text/html;charset=UTF-8" /> <remove fileExtension=".css" /> <mimeMap fileExtension=".css" mimeType="text/css" /> <remove fileExtension=".woff" /> <mimeMap fileExtension=".woff" mimeType="application/font-woff" /> <remove fileExtension=".woff2" /> <mimeMap fileExtension=".woff2" mimeType="application/font-woff2" /> <remove fileExtension=".js" /> <mimeMap fileExtension=".js" mimeType="application/x-javascript;charset=UTF-8" /> <remove fileExtension=".svg" /> <mimeMap fileExtension=".svg" mimeType="image/svg+xml" /> <!-- BEGIN gzip remove application/x-gzip or maybe to changed text/html --> <remove fileExtension=".gz" /> <mimeMap fileExtension=".gz" mimeType="application/x-gzip;charset=UTF-8" /> <!-- END gzip remove application/x-gzip or maybe to changed text/html --> </staticContent> <!-- after caching open, the specified caching type (htm,html,shtm,shtml,css,woff,js,svg,gz) returns 200 when the page is opened for the first time, and 304 when the page is refreshed by F5. If it is not 304 (after refresh), the local storage of the browser is not read, proving that there is no cache or that the client browser has disabled the cache. but some pages will automatically survive temporary merge files, such as js and css,/wp-content/cache/autoptimize/css/autoptimize_6c9ad88b45e32ed5bdbe0ef0a7a8a429.css, it still remains at 200,because itundefineds just been regenerated (new and different). --> <caching enabled="true" enableKernelCache="true"> <profiles> <!-- in the web.config file, iis uses the output caching module and the caching directive to control caching. for the sample application, you can enable caching for .html files for a maximum of 30 days. for .php files, ensure that no caching is performed at all with the code. --> <add extension=".php" policy="DisableCache" kernelCachePolicy="DisableCache" /> <add extension=".htm" policy="CacheForTimePeriod" kernelCachePolicy="CacheForTimePeriod" duration="30:00:00:00" /> <add extension=".html" policy="CacheForTimePeriod" kernelCachePolicy="CacheForTimePeriod" duration="30:00:00:00" /> <add extension=".shtm" policy="CacheForTimePeriod" kernelCachePolicy="CacheForTimePeriod" duration="30:00:00:00" /> <add extension=".shtml" policy="CacheForTimePeriod" kernelCachePolicy="CacheForTimePeriod" duration="30:00:00:00" /> <add extension=".css" policy="CacheForTimePeriod" kernelCachePolicy="CacheForTimePeriod" duration="30:00:00:00" /> <add extension=".woff" policy="CacheForTimePeriod" kernelCachePolicy="CacheForTimePeriod" duration="30:00:00:00" /> <add extension=".js" policy="CacheForTimePeriod" kernelCachePolicy="CacheForTimePeriod" duration="30:00:00:00" /> <add extension=".svg" policy="CacheForTimePeriod" kernelCachePolicy="CacheForTimePeriod" duration="30:00:00:00" /> <add extension=".gz" policy="CacheForTimePeriod" kernelCachePolicy="CacheForTimePeriod" duration="30:00:00:00" /> </profiles> </caching> <!-- END Browser cache and mimeMappings --> </system.webServer> |
//iis配置编辑器设置



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | //2012/2012r2 ctrl + f + x + a 运行超级管理员cmd //2016/2019 win键 + x + a 运行超级管理员cmd //gzip json %windir%/system32/inetsrv/appcmd.exe set config -section:system.webServer/serverRuntime /frequentHitThreshold:1 %windir%/system32/inetsrv/appcmd.exe set config -section:system.webServer/serverRuntime /frequentHitTimePeriod:01:00:00 /commit:apphost //iis管理器 - 配置编辑器 system.webServer/httpCompression "dynamicTypes"元素编辑项 //gzip json atom+xml atom+xml;charset=utf-8 image/svg+xml application/x-gzip;charset=UTF-8 application/font-woff application/font-woff2 %windir%/system32/inetsrv/appcmd.exe set config -section:system.webServer/httpCompression /+"dynamicTypes.[mimeType='application/json',enabled='True']" /commit:apphost %windir%/system32/inetsrv/appcmd.exe set config -section:system.webServer/httpCompression /+"dynamicTypes.[mimeType='application/atom%u002bxml',enabled='True']" /commit:apphost %windir%/system32/inetsrv/appcmd.exe set config -section:system.webServer/httpCompression /+"dynamicTypes.[mimeType='application/atom%u002bxml;charset=utf-8',enabled='True']" /commit:apphost %windir%/system32/inetsrv/appcmd.exe set config -section:system.webServer/httpCompression /+"dynamicTypes.[mimeType='image/svg%u002bxml',enabled='True']" /commit:apphost %windir%/system32/inetsrv/appcmd.exe set config -section:system.webServer/httpCompression /+"dynamicTypes.[mimeType='application/x-gzip;charset=utf-8',enabled='True']" /commit:apphost %windir%/system32/inetsrv/appcmd.exe set config -section:system.webServer/httpCompression /+"dynamicTypes.[mimeType='application/font-woff',enabled='True']" /commit:apphost %windir%/system32/inetsrv/appcmd.exe set config -section:system.webServer/httpCompression /+"dynamicTypes.[mimeType='application/font-woff2',enabled='True']" /commit:apphost |