Apache到IIS的web.config?

时间:2018-12-13 15:42:45

标签: caching iis server web-config

我曾经使用过Apache或NGINX,但是一个新客户端的网站位于Microsoft IIS服务器上,该服务器使用web.config来添加缓存。

我通常使用以下设置:

### Begin Caching Performance ###
# Use UTF-8 encoding for anything served text/plain or text/html
AddDefaultCharset UTF-8
# Force UTF-8 for a number of file formats
<IfModule mod_mime.c>
    AddCharset UTF-8 .atom .css .js .json .rss .vtt .xml
</IfModule>

# FileETag None is not enough for every server.
<IfModule mod_headers.c>
Header unset ETag
</IfModule>

# Since we’re sending far-future expires, we don’t need ETags for static content.
FileETag None

<IfModule mod_alias.c>
    <FilesMatch "\.(html|htm|rtf|rtx|txt|xsd|xsl|xml)$">
    <IfModule mod_headers.c>
        Header unset Pragma
        Header append Cache-Control "public"
        Header unset Last-Modified
    </IfModule>
</FilesMatch>

<FilesMatch "\.(css|htc|js|asf|asx|wax|wmv|wmx|avi|bmp|class|divx|doc|docx|eot|exe|gif|gz|gzip|ico|jpg|jpeg|jpe|json|mdb|mid|midi|mov|qt|mp3|m4a|mp4|m4v|mpeg|mpg|mpe|mpp|otf|odb|odc|odf|odg|odp|ods|odt|ogg|pdf|png|pot|pps|ppt|pptx|ra|ram|svg|svgz|swf|tar|tif|tiff|ttf|ttc|wav|wma|wri|xla|xls|xlsx|xlt|xlw|zip)$">
        <IfModule mod_headers.c>
            Header unset Pragma
            Header append Cache-Control "public"
        </IfModule>
    </FilesMatch>
</IfModule>

# Gzip Compression
<IfModule mod_deflate.c>
    # Force compression for mangled headers.
    <IfModule mod_setenvif.c>
        <IfModule mod_headers.c>
            SetEnvIfNoCase ^(Accept-EncodXng|X-cept-Encoding|X{15}|~{15}|-{15})$ ^((gzip|deflate)\s*,?\s*)+|[X~-]{4,13}$ HAVE_Accept-Encoding
            RequestHeader append Accept-Encoding "gzip,deflate" env=HAVE_Accept-Encoding
            # Don’t compress images and other uncompressible content
            SetEnvIfNoCase Request_URI \
            \.(?:gif|jpe?g|png|rar|zip|exe|flv|mov|wma|mp3|avi|swf|mp?g|mp4|webm|webp|pdf)$ no-gzip dont-vary
        </IfModule>
    </IfModule>
    # Compress all output labeled with one of the following MIME-types
    <IfModule mod_filter.c>
    AddOutputFilterByType DEFLATE "application/atom+xml" \
                                "application/javascript" \
                                "application/json" \
                                "application/ld+json" \
                                "application/manifest+json" \
                                "application/rdf+xml" \
                                "application/rss+xml" \
                                "application/schema+json" \
                                "application/vnd.geo+json" \
                                "application/vnd.ms-fontobject" \
                                "application/x-font-ttf" \
                                "application/x-javascript" \
                                "application/x-web-app-manifest+json" \
                                "application/xhtml+xml" \
                                "application/xml" \
                                "font/eot" \
                                "font/opentype" \
                                "image/bmp" \
                                "image/svg+xml" \
                                "image/vnd.microsoft.icon" \
                                "image/x-icon" \
                                "text/cache-manifest" \
                                "text/css" \
                                "text/html" \
                                "text/javascript" \
                                "text/plain" \
                                "text/vcard" \
                                "text/vnd.rim.location.xloc" \
                                "text/vtt" \
                                "text/x-component" \
                                "text/x-cross-domain-policy" \
                                "text/xml"
    </IfModule>
    <IfModule mod_headers.c>
        Header append Vary: Accept-Encoding
    </IfModule>
</IfModule>

<IfModule mod_mime.c>
    AddType text/html .html_gzip
    AddEncoding gzip .html_gzip
</IfModule>
<IfModule mod_setenvif.c>
    SetEnvIfNoCase Request_URI \.html_gzip$ no-gzip
</IfModule>

# Expires headers
<IfModule mod_expires.c>
    ExpiresActive on
    ExpiresDefault                                  "access plus 1 month"

# cache.appcache needs re-requests in FF 3.6
    ExpiresByType text/cache-manifest               "access plus 0 seconds"

# CSS
    ExpiresByType text/css                          "access plus 1 year"

# Data interchange
    ExpiresByType application/json                  "access plus 0 seconds"
    ExpiresByType application/xml                   "access plus 0 seconds"
    ExpiresByType text/xml                          "access plus 0 seconds"

# Favicon (cannot be renamed!)
    ExpiresByType image/x-icon                      "access plus 1 week"

# HTML components (HTCs)
    ExpiresByType text/x-component                  "access plus 1 month"

# HTML
    ExpiresByType text/html                         "access plus 0 seconds"

# JavaScript
    ExpiresByType application/javascript            "access plus 1 year"

# Manifest files
    ExpiresByType application/x-web-app-manifest+json   "access plus 0 seconds"
    ExpiresByType text/cache-manifest                   "access plus 0 seconds"

# Media
    ExpiresByType image/gif                         "access plus 1 month"
    ExpiresByType image/jpeg                        "access plus 1 month"
    ExpiresByType image/png                         "access plus 1 month"
    ExpiresByType video/mp4                         "access plus 1 month"
    ExpiresByType audio/ogg                         "access plus 1 month"
    ExpiresByType video/ogg                         "access plus 1 month"
    ExpiresByType video/webm                        "access plus 1 month"

# Web feeds
    ExpiresByType application/atom+xml              "access plus 1 hour"
    ExpiresByType application/rss+xml                "access plus 1 hour"

# Web fonts
    ExpiresByType application/font-woff             "access plus 1 month"
    ExpiresByType application/font-woff2            "access plus 1 month"
    ExpiresByType application/vnd.ms-fontobject     "access plus 1 month"
    ExpiresByType application/x-font-ttf            "access plus 1 month"
    ExpiresByType font/opentype                     "access plus 1 month"
    ExpiresByType image/svg+xml                     "access plus 1 month"
</IfModule>

# Send CORS headers if browsers request them; enabled by default for images.
<IfModule mod_setenvif.c>
    <IfModule mod_headers.c>
        # mod_headers
        <FilesMatch "\.(gif|png|jpe?g|svg|svgz|ico|webp)$">
            SetEnvIf Origin ":" IS_CORS
            Header set Access-Control-Allow-Origin "*" env=IS_CORS
        </FilesMatch>
    </IfModule>
</IfModule>

# Webfont access
<IfModule mod_headers.c>
    <FilesMatch "\.(tt[cf]|otf|eot|woff|woff2|font.css|css|js)$">
        Header set Access-Control-Allow-Origin "*"
    </FilesMatch>
</IfModule>
### End Caching Performance ###

因此,我想将这个或至少一些GZIP和到期标题添加到此网站,以使其性能更高。有谁知道我将如何转换或至少将我指向正确的方向,以将上述性能和缓存代码转换为Microsoft IIS的web.config?

预先感谢您提供更多信息!

1 个答案:

答案 0 :(得分:0)

最后,我像这样配置了web.config,我直接通过web.config进行操作的原因是客户端无法访问IIS控制面板,或者托管公司也不想提供它

这是我最终想出的代码:

<!-- General Optimisation-->
<directoryBrowse enabled="false"/>
<!-- Security Headers -->
<httpProtocol>
    <customHeaders>
    <add name="Security-By" value="Sandhills Studio"/>
    <add name="Content-Security-Policy" value="img-src 'self' https: data: blob:; font-src 'self' https: data:;"/>
    <add name="X-Frame-Options" value="SAMEORIGIN"/>
    <add name="X-XSS-Protection" value="1; mode=block"/>
    <add name="Referrer-Policy" value="no-referrer-when-downgrade"/>
    <add name="Expect-CT" value="max-age=86400,enforce"/>
    <add name="Feature-Policy" value="fullscreen *;camera 'none';microphone 'none'"/>
    <add name="X-Content-Type-Options" value="nosniff"/>
    <add name="Strict-Transport-Security" value="max-age=15552000; includeSubDomains; preload"/>
    <!--Remove Headers-->
    <remove name="X-Powered-By"/>
    <remove name="X-Powered-By-Plesk"/>
    <remove name="Pragma"/>
    <remove name="ETag"/>
    </customHeaders>
</httpProtocol>
<!-- GZip static file content -->
<httpCompression directory="%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files" minFileSizeForComp="512">
    <scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll" staticCompressionLevel="10"/>
    <dynamicTypes>
        <add mimeType="text/*" enabled="true"/>
        <add mimeType="message/*" enabled="true"/>
        <add mimeType="application/javascript" enabled="true"/>
        <add mimeType="application/json" enabled="true"/>
        <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="*/*" enabled="false"/>
    </dynamicTypes>
    <staticTypes>
        <add mimeType="text/*" enabled="true"/>
        <add mimeType="message/*" enabled="true"/>
        <add mimeType="application/atom+xml" enabled="true"/>
        <add mimeType="application/javascript" enabled="true"/>
        <add mimeType="application/json" enabled="true"/>
        <add mimeType="application/ld+json" enabled="true"/>
        <add mimeType="application/manifest+json" enabled="true"/>
        <add mimeType="application/rdf+xml" enabled="true"/>
        <add mimeType="application/rss+xml" enabled="true"/>
        <add mimeType="application/schema+json" enabled="true"/>
        <add mimeType="application/vnd.geo+json" enabled="true"/>
        <add mimeType="application/vnd.ms-fontobject" enabled="true"/>
        <add mimeType="application/x-font-ttf" enabled="true"/>
        <add mimeType="application/x-javascript" enabled="true"/>
        <add mimeType="application/x-web-app-manifest+json" enabled="true"/>
        <add mimeType="application/xhtml+xml" enabled="true"/>
        <add mimeType="application/xaml+xml" enabled="true"/>
        <add mimeType="application/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="font/eot" enabled="true"/>
        <add mimeType="font/opentype" enabled="true"/>
        <add mimeType="image/bmp" enabled="true"/>
        <add mimeType="image/svg+xml" enabled="true"/>
        <add mimeType="image/vnd.microsoft.icon" enabled="true"/>
        <add mimeType="image/x-icon" enabled="true"/>
        <add mimeType="*/*" enabled="false"/>
    </staticTypes>
</httpCompression>
<urlCompression doStaticCompression="true" doDynamicCompression="true"/>
<!-- Clinet Cache Control -->
<staticContent>
     <!-- Set expire headers to 30 days for static content-->
    <clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="30.00:00:00" setEtag="false"/>
    <!-- use utf-8 encoding for anything served text/plain or text/html -->
    <remove fileExtension=".air"/>
    <mimeMap fileExtension=".air" mimeType="application/vnd.adobe.air-application-installer-package+zip"/>
    <remove fileExtension=".css"/>
    <mimeMap fileExtension=".css" mimeType="text/css"/>
    <remove fileExtension=".js"/>
    <mimeMap fileExtension=".js" mimeType="text/javascript"/>
    <remove fileExtension=".json"/>
    <mimeMap fileExtension=".json" mimeType="application/json"/>
    <remove fileExtension=".rss"/>
    <mimeMap fileExtension=".rss" mimeType="application/rss+xml; charset=UTF-8"/>
    <remove fileExtension=".html"/>
    <mimeMap fileExtension=".html" mimeType="text/html; charset=UTF-8"/>
    <remove fileExtension=".xml"/>
    <mimeMap fileExtension=".xml" mimeType="application/xml; charset=UTF-8"/>
    <!-- HTML5 Audio/Video mime types-->
    <remove fileExtension=".mp3"/>
    <mimeMap fileExtension=".mp3" mimeType="audio/mpeg"/>
    <remove fileExtension=".mp4"/>
    <mimeMap fileExtension=".mp4" mimeType="video/mp4"/>
    <remove fileExtension=".ogg"/>
    <mimeMap fileExtension=".ogg" mimeType="audio/ogg"/>
    <remove fileExtension=".ogv"/>
    <mimeMap fileExtension=".ogv" mimeType="video/ogg"/>
    <remove fileExtension=".webm"/>
    <mimeMap fileExtension=".webm" mimeType="video/webm"/>
    <!-- Proper svg serving. Required for svg webfonts on iPad -->
    <remove fileExtension=".svg"/>
    <mimeMap fileExtension=".svg" mimeType="image/svg+xml"/>
    <remove fileExtension=".svgz"/>
    <mimeMap fileExtension=".svgz" mimeType="image/svg+xml"/>
    <!-- Remove default IIS mime type for .eot which is application/octet-stream -->
    <remove fileExtension=".eot"/>
    <mimeMap fileExtension=".eot" mimeType="application/vnd.ms-fontobject"/>
    <remove fileExtension=".ttf"/>
    <mimeMap fileExtension=".ttf" mimeType="application/x-font-ttf"/>
    <remove fileExtension=".ttc"/>
    <mimeMap fileExtension=".ttc" mimeType="application/x-font-ttf"/>
    <remove fileExtension=".otf"/>
    <mimeMap fileExtension=".otf" mimeType="font/opentype"/>
    <remove fileExtension=".woff"/>
    <mimeMap fileExtension=".woff" mimeType="application/x-font-woff"/>
    <remove fileExtension=".woff2"/>
    <mimeMap fileExtension=".woff2" mimeType="application/x-font-woff2"/>
    <remove fileExtension=".less"/>
    <mimeMap fileExtension=".less" mimeType="text/css"/>
    <remove fileExtension=".crx"/>
    <mimeMap fileExtension=".crx" mimeType="application/x-chrome-extension"/>
    <remove fileExtension=".xpi"/>
    <mimeMap fileExtension=".xpi" mimeType="application/x-xpinstall"/>
    <remove fileExtension=".safariextz"/>
    <mimeMap fileExtension=".safariextz" mimeType="application/octet-stream"/>
    <!-- Flash Video mime types-->
    <remove fileExtension=".flv"/>
    <mimeMap fileExtension=".flv" mimeType="video/x-flv"/>
    <remove fileExtension=".f4v"/>
    <mimeMap fileExtension=".f4v" mimeType="video/mp4"/>
    <!-- Assorted types -->
    <remove fileExtension=".ico"/>
    <mimeMap fileExtension=".ico" mimeType="image/x-icon"/>
    <remove fileExtension=".webp"/>
    <mimeMap fileExtension=".webp" mimeType="image/webp"/>
    <remove fileExtension=".htc"/>
    <mimeMap fileExtension=".htc" mimeType="text/x-component"/>
    <remove fileExtension=".vcf"/>
    <mimeMap fileExtension=".vcf" mimeType="text/x-vcard"/>
    <remove fileExtension=".torrent"/>
    <mimeMap fileExtension=".torrent" mimeType="application/x-bittorrent"/>
    <remove fileExtension=".cur"/>
    <mimeMap fileExtension=".cur" mimeType="image/x-icon"/>
    <remove fileExtension=".webapp"/>
    <mimeMap fileExtension=".webapp" mimeType="application/x-web-app-manifest+json; charset=UTF-8"/>
</staticContent>

可悲的是,SVG并未被压缩,即使列出的级别较低,必须将其包括在内,因为IIS默认情况下无法识别此MIME类型。

如果有人有任何建议,请随时告诉我!