user2966445
user2966445

Reputation: 1344

Asp.Net Core - Enable HttpCompression in Shared Hosting Environment?

I'm using a shared host running a ASP.Net Core 2.0 web app. When I run the app on localhost, I can see HttpCompression is working just fine (content-encoding: gzip) response header is returned for JS files).

After I deploy to the shared host, the content-encoding: gzip response is no longer there. I tried adding various httpCompression/urlCompression settings in the web.config, but the hosting company says these settings are disabled for my plan.

Is there any other way to make Gzip compression work, or do I have to use a hosting plan where it is enabled in IIS?

Edit: I'm also using the ResponseCompression middleware as part of ASP.Net Core and the content-encoding response headers still do not appear. Configure code is as follows:

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddLogging();

        services.AddResponseCompression(options =>
        {
            options.Providers.Add<GzipCompressionProvider>();
        });

...
}

        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {        
        if (env.IsDevelopment())
        {
            loggerFactory.AddDebug(LogLevel.Debug);
            app.UseDeveloperExceptionPage();
            app.UseBrowserLink();
        }
        else
        {
            app.UseExceptionHandler("/error");
        }

        app.UseStatusCodePagesWithRedirects("/error/{0}");
        app.UseResponseCompression();

        app.UseStaticFiles(new StaticFileOptions
        {
            OnPrepareResponse = ctx =>
            {
                ctx.Context.Response.Headers.Append("Cache-Control", "public, max-age=604800");
            }
        });

        app.UseAuthentication();

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

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

        var options = new RewriteOptions()
            .AddRedirectToHttps();

        app.UseRewriter(options);
    }

Upvotes: 1

Views: 1125

Answers (1)

pfx
pfx

Reputation: 23314

Use the Response Compression Middleware; add a reference to the Microsoft.AspNetCore.ResponseCompression or Microsoft.AspNetCore.All NuGet package. See docs.

Set it up via:

WebHost.CreateDefaultBuilder(args)
    .ConfigureServices(services =>
    {
        services.AddResponseCompression();
    })
    .Configure(app =>
    {
        app.UseResponseCompression();

        // ...
    })
    .Build();

Edit: In a comment below you mention that your website is on https which is the reason why response compression is off by default to prevent CRIME and BREACH attacks. You can re-enable this via the EnableForHttps option.

services.AddResponseCompression(options =>
{
options.EnableForHttps = true;
options.Providers.Add<GzipCompressionProvider>();
});

Upvotes: 1

Related Questions