ead*_*dam 22 asp.net-core-mvc asp.net-core
在使用自托管环境发布ASP.NET Core网站时,是否有提供gzip静态功能的方法?
Ily*_*dik 15
还有另一种方法来提供替换步骤2和3的gzip文件.它基本上是完全相同的想法,但有一个nuget 包可以为您提供所有功能.它基本上检查是否匹配所请求的文件.gz或.br文件.如果存在,则返回适当的标题.它确实验证请求是否具有相应算法的标头.如果你想自己编译它的Github代码就在这里.
在官方存储库中还有一个问题需要支持,因此我真的希望Microsoft能够使用标准插件来实现这一点,因为现在使用它是相当普遍和合乎逻辑的.
我想我找到了最优化的压缩内容服务方式.主要思想是预压缩文件,因为默认的ASP.NET 5方法是使用gulp来构建js,所以这很容易做到:
gulp.task("buildApplication:js", function () {
return gulp.src(...)
...
.pipe(gzip())
...
});
Run Code Online (Sandbox Code Playgroud)
这将在您的包文件夹中生成类似libraries.js.gz的内容
我们需要添加Content-Encoding和更改Content-Type默认值application/x-gzip,application/javascript因为并非所有浏览器都足够智能,可以正确读取jsx-gzip
app.UseStaticFiles(new StaticFileOptions
{
OnPrepareResponse = context =>
{
if (headers.ContentType.MediaType == "application/x-gzip")
{
if (context.File.Name.EndsWith("js.gz"))
{
headers.ContentType = new MediaTypeHeaderValue("application/javascript");
}
else if (context.File.Name.EndsWith("css.gz"))
{
headers.ContentType = new MediaTypeHeaderValue("text/css");
}
context.Context.Response.Headers.Add("Content-Encoding", "gzip");
}
}
});
Run Code Online (Sandbox Code Playgroud)
现在所有的CPU周期都没有浪费,因为它们始终都是gzip相同的内容,这是提供文件的最佳性能.为了进一步改善它,所有js必须在gzipping之前进行整理和缩小.另一个升级是在相同的OnPrepareResponse中将CacheControl max age设置为缓存一年并添加asp-append-version="true"cshtml.
PS如果您将在IIS后面托管,您可能需要关闭js和css的静态压缩而不是双重压缩,我不确定它在这种情况下会如何表现.
这是来自Ilyas 的方法3的固定版本,它与ASP.NET Core 1 RTM一起使用,它提供预压缩的javascript文件:
app.UseStaticFiles(new StaticFileOptions
{
OnPrepareResponse = context =>
{
IHeaderDictionary headers = context.Context.Response.Headers;
string contentType = headers["Content-Type"];
if (contentType == "application/x-gzip")
{
if (context.File.Name.EndsWith("js.gz"))
{
contentType = "application/javascript";
}
else if (context.File.Name.EndsWith("css.gz"))
{
contentType = "text/css";
}
headers.Add("Content-Encoding", "gzip");
headers["Content-Type"] = contentType;
}
}
});
Run Code Online (Sandbox Code Playgroud)
@Ilya的答案非常好,但如果您不使用Gulp,这里有两个选择.
在ASP.NET Core BasicMiddlware存储库中,您可以找到(在编写本文时)响应压缩中间件的拉取请求(PR).您可以下载代码并将其添加到您IApplicationBuilder这样(在撰写本文时):
public void Configure(IApplicationBuilder app)
{
app.UseResponseCompression(
new ResponseCompressionOptions()
{
MimeTypes = new string[] { "text/plain" }
});
// ...Omitted
}
Run Code Online (Sandbox Code Playgroud)
IIS(Internet Information Server)具有本机静态文件模块,该模块独立于您在本文中了解到的ASP.NET静态文件中间件组件.由于ASP.NET模块在IIS本机模块之前运行,因此它们优先于IIS本机模块.从ASP.NET Beta 7开始,IIS主机已更改,因此ASP.NET未处理的请求将返回空404响应,而不是允许IIS本机模块运行.要选择运行IIS本机模块,请将以下调用添加到Startup.Configure的末尾.
public void Configure(IApplicationBuilder app)
{
// ...Omitted
// Enable the IIS native module to run after the ASP.NET middleware components.
// This call should be placed at the end of your Startup.Configure method so that
// it doesn't interfere with other middleware functionality.
app.RunIISPipeline();
}
Run Code Online (Sandbox Code Playgroud)
然后在你的Web.config中使用以下设置打开GZIP压缩(请注意,我包含了一些额外的行来压缩像.json文件这样的东西,否则它们将被IIS解压缩):
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<!-- httpCompression - GZip compress static file content. Overrides the server default which only compresses static
files over 2700 bytes. See http://zoompf.com/blog/2012/02/lose-the-wait-http-compression and
http://www.iis.net/configreference/system.webserver/httpcompression -->
<!-- minFileSizeForComp - The minimum file size to compress. -->
<httpCompression directory="%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files" minFileSizeForComp="1024">
<scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll" />
<dynamicTypes>
<add mimeType="text/*" enabled="true" />
<add mimeType="message/*" enabled="true" />
<add mimeType="application/x-javascript" enabled="true" />
<!-- Compress XML files -->
<add mimeType="application/xml" enabled="true" />
<!-- Compress JavaScript files -->
<add mimeType="application/javascript" enabled="true" />
<!-- Compress JSON files -->
<add mimeType="application/json" enabled="true" />
<!-- Compress SVG files -->
<add mimeType="image/svg+xml" enabled="true" />
<!-- Compress RSS feeds -->
<add mimeType="application/rss+xml" enabled="true" />
<!-- Compress Atom feeds -->
<add mimeType="application/atom+xml" enabled="true" />
<add mimeType="*/*" enabled="false" />
</dynamicTypes>
<staticTypes>
<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" />
<!-- Compress ICO icon files (Note that most .ico files are uncompressed but there are some that can contain
PNG compressed images. If you are doing this, remove this line). -->
<add mimeType="image/x-icon" enabled="true" />
<!-- Compress XML files -->
<add mimeType="application/xml" enabled="true" />
<add mimeType="application/xml; charset=UTF-8" enabled="true" />
<!-- Compress JavaScript files -->
<add mimeType="application/javascript" enabled="true" />
<!-- Compress JSON files -->
<add mimeType="application/json" enabled="true" />
<!-- Compress SVG files -->
<add mimeType="image/svg+xml" enabled="true" />
<!-- Compress EOT font files -->
<add mimeType="application/vnd.ms-fontobject" enabled="true" />
<!-- Compress TTF font files - application/font-ttf will probably be the new correct MIME type. IIS still uses application/x-font-ttf. -->
<!--<add mimeType="application/font-ttf" enabled="true" />-->
<add mimeType="application/x-font-ttf" enabled="true" />
<!-- Compress OTF font files - application/font-opentype will probably be the new correct MIME type. IIS still uses font/otf. -->
<!--<add mimeType="application/font-opentype" enabled="true" />-->
<add mimeType="font/otf" enabled="true" />
<!-- Compress RSS feeds -->
<add mimeType="application/rss+xml" enabled="true" />
<add mimeType="application/rss+xml; charset=UTF-8" enabled="true" />
<add mimeType="*/*" enabled="false" />
</staticTypes>
</httpCompression>
<!-- Enable gzip and deflate HTTP compression. See http://www.iis.net/configreference/system.webserver/urlcompression
doDynamicCompression - enables or disables dynamic content compression at the site, application, or folder level.
doStaticCompression - enables or disables static content compression at the site, application, or folder level.
dynamicCompressionBeforeCache - specifies whether IIS will dynamically compress content that has not been cached.
When the dynamicCompressionBeforeCache attribute is true, IIS dynamically compresses
the response the first time a request is made and queues the content for compression.
Subsequent requests are served dynamically until the compressed response has been
added to the cache directory. Once the compressed response is added to the cache
directory, the cached response is sent to clients for subsequent requests. When
dynamicCompressionBeforeCache is false, IIS returns the uncompressed response until
the compressed response has been added to the cache directory.
Note: This is set to false in Debug mode to enable Browser Link to work when debugging.
The value is set to true in Release mode (See web.Release.config).-->
<urlCompression doDynamicCompression="true" doStaticCompression="true" dynamicCompressionBeforeCache="false" />
</system.webServer>
</configuration>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6523 次 |
| 最近记录: |