asp.net core 2.2 brotli compression not working on IIS?

Viewed 1555

I read in Asp.Net Core 2.2 documentation, brotli compression is added to 2.2.

I have app.UseResponseCompression(); and services.AddResponseCompression(); in my Startup.cs but when I tried it on my system with latest Chrome browser, I see only gzip compression at response headers. I host my Asp.Net Core 2.2 app on Windows Server 2012 IIS web server.

What should I config to get working brotli compression?

dev tools screenshot

2 Answers

If you're hosting in IIS then that means IIS is the webserver that's communicating with the browser. It sits in front of your ASP.NET Core app and proxies all requests and responses, but it doesn't support Brotli by default.

You need to configure response compression with IIS. There's an extension available for adding Brotli and Zlib support: https://docs.microsoft.com/en-us/iis/extensions/iis-compression/iis-compression-overview

You can also run your ASP.NET Core app without IIS.

You mentioned that your application is hosted in IIS and it says in the link you attached

Use Response Compression Middleware when you're unable to use IIS Dynamic Compression Module

So if you use IIS you can remove that middleware and use the out of the box IIS Compression module.

The steps you need to take are:

  1. Install brotli on your webserver - just follow the instructions here https://docs.microsoft.com/en-us/iis/extensions/iis-compression/iis-compression-overview#installing-iis-compression%5D
  2. Remove the compression middleware
  3. On your applicationHost.conf make sure that brotli exist under gzip
  4. Comment out the gzip option in applicationHost.conf because 98% of the browsers support brotli compression and in old version of IIS gzip is selected instead of brotli because of the order in the Request Header (e.g gzip deflate br).
Related