简体   繁体   English

Gzip 压缩中间件在 asp.net core 2.0 中不起作用

[英]Gzip compression middleware not working in asp.net core 2.0

I am unable to get Gzip compression middleware to work in asp.net core 2.0 (or 2.1).我无法让 Gzip 压缩中间件在 asp.net core 2.0(或 2.1)中工作。 I created a new project using the "Blank" Web Application template and have the following code in Startup.cs:我使用“Blank”Web 应用程序模板创建了一个新项目,并在 Startup.cs 中有以下代码:

public void ConfigureServices(IServiceCollection services)
{
    services.AddResponseCompression();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    app.UseResponseCompression();

    app.Run(async (context) =>
        {
            context.Response.Headers.Add("Content-Type", "text/plain");
            await context.Response.WriteAsync("Hello World!");
        });
}

I verified that Chrome is sending "Accept-Encoding: gzip, deflate, br" in the request.我确认 Chrome 在请求中发送了“Accept-Encoding: gzip, deflate, br”。

Accept-Encoding接受编码

However, the server is not gzip-encoding the response:但是,服务器不会对响应进行 gzip 编码:

Content-Encoding内容编码

What am I doing wrong?我究竟做错了什么?

I found this discussion, and tried everything from there, but it didn't help.我找到了这个讨论,并从那里尝试了一切,但没有帮助。

I faced a similar problem (asp.net core on Mac)我遇到了类似的问题(Mac 上的 asp.net core)

Solution was to make sure response compression was called BEFORE static file serving.解决方案是确保在静态文件服务之前调用响应压缩。

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseCookiePolicy();

        // ordering matters here
        app.UseResponseCompression();
        app.UseStaticFiles();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });
    }

After ordering it correctly I could see it in chrome under encoding br (Brotli Compression):正确订购后,我可以在编码br (Brotli 压缩)下的 chrome 中看到它:

在此处输入图片说明

TL;DR: The code is fine, the issue with an environment. TL;DR:代码很好,环境问题。

First, please find out if you have any antivirus that intercepts incoming HTTP traffic.首先,请查明您是否有任何拦截传入 HTTP 流量的防病毒软件。 In my case, it was ESET Endpoint Antivirus.就我而言,它是 ESET Endpoint Antivirus。 Second, Check your response for the presence of header Vary: Accept-Encoding .其次,检查您的响应是否存在标头Vary: Accept-Encoding That indicates that Kestrel processed your request correctly and paid attention to Accept-Encoding request header.这表明 Kestrel 正确处理了您的请求并注意了 Accept-Encoding 请求标头。

If you do have antivirus what happens is that it sniffs incoming unencrypted traffic, sees gzip header and unpack message to check for malware or whatever.如果你有防病毒软件,它会嗅探传入的未加密流量,查看 gzip 标头和解包消息以检查恶意软件或其他任何内容。 Then it passes the decompressed response to a browser and removes gzip content-encoding header.然后它将解压缩的响应传递给浏览器并删除 gzip 内容编码标头。

You should turn off antivirus for some time, or test this feature on the environment without one.您应该关闭防病毒一段时间,或者在没有安装的环境中测试此功能。

From ASP.NET Core Middleware :ASP.NET 核心中间件

The following example demonstrates a middleware ordering where requests for static files are handled by the static file middleware before the response compression middleware.以下示例演示了一个中间件排序,其中静态文件请求在响应压缩中间件之前由静态文件中间件处理。 Static files are not compressed with this ordering of the middleware.静态文件不会按照中间件的这种顺序进行压缩。

Below a screen capture of Fiddler showing the request and response with corresponding headers:下面是 Fiddler 的屏幕截图,显示了带有相应标头的请求和响应:

在此处输入图片说明

I was facing the same problem.我面临同样的问题。

My observation is, Fiddler somehow doesn't show Content-Encoding, if it is done through ASP.net core middlewear.我的观察是,如果 Fiddler 是通过 ASP.net 核心中间件完成的,那么它以某种方式不显示内容编码。 I am not sure why.我不知道为什么。

But If I look at the same request in Chrome debugger, I am able to see that information with the compressed data.但是,如果我在 Chrome 调试器中查看相同的请求,我可以看到包含压缩数据的信息。

  1. First row in image is without compression图像中的第一行没有压缩
  2. Second row in image is with compression图像中的第二行是压缩的

在此处输入图片说明

If you use Fiddler, make sure the "Decode" function disabled or Fiddler will auto decode.如果您使用 Fiddler,请确保禁用“解码”功能,否则 Fiddler 将自动解码。 禁用解码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM