DEV Community

loading...

Discussion on: .NET, NGINX, Kestrel, and React with a Reverse Proxy on Linux Ubuntu

Collapse
slavius profile image
Slavius • Edited

Hi Chris,
for proper working and some advanced scenarios you should add the following to your .Net app behind proxy:

public void ConfigureServices(IServiceCollection services) {
    ...
    services.Configure<ForwardedHeadersOptions>(options => {
        options.ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto;
        }
    );
    ...
}
Enter fullscreen mode Exit fullscreen mode

Also if you have HSTS and/or CORS settings applied you should remove them as it will become responsibility of your front-end proxy server to handle these:

public void ConfigureServices(IServiceCollection services) {
    ...
    //services.AddHsts(options => {
    //    options.MaxAge = TimeSpan.FromDays(365);
    //});

    //services.AddCors(options =>
    //{
    //    options.AddDefaultPolicy(builder =>
    //    {
    //        builder.AllowAnyHeader();
    //        builder.AllowCredentials();
    //        builder.AllowAnyMethod();
    //        builder.AllowAnyOrigin();
    //    });
    //});
    ...
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env) {
    ...
    //app.UseCors();
    //app.UseHsts();
    ...
}
Enter fullscreen mode Exit fullscreen mode
Collapse
fullstackchris profile image
Chris Frewin Author

Hi Slavius, thanks for the additions here!