liveBook · Manning

Reverse proxy

As explained in ASP.NET Core in Action, 3e

A reverse proxy is a server that receives client requests and forwards them to backend applications or services on the clients' behalf.

A reverse proxy receives requests from the internet and forwards them to the appropriate web server. In an ASP.NET Core deployment, it can sit in front of Kestrel: clients reach the proxy, while Kestrel is exposed only to the proxy. This is common, but not required—Kestrel can also receive internet requests directly. section 2.4.2 section 27.1

Figure 27.1 The hosting model for ASP.NET Core gives flexibility. The same application can run exposed directly to the network, behind various reverse proxies without modification, and even inside the IIS process.
CH27_F01_Lock3

The hosting diagram shows the practical choice: the same application can run directly on the network or behind different reverse proxies without changing its application code. section 27.1

Put a proxy in front of an app when its extra responsibilities are useful:

  • Security: a proxy designed for public traffic stands between the internet and the underlying web server. section 27.1
  • Performance: a proxy can cache responses. section 27.1
  • Process management: some proxies can monitor an app and respond when it crashes. section 27.1

A Windows or Linux deployment

On Windows, IIS commonly acts as the reverse proxy; a basic deployment involves publishing the app, setting up an IIS application pool and website, and copying the published files to its hosting folder. On Linux, NGINX or Apache can fill the proxy role. The app generally handles requests the same way regardless of which proxy sits in front of it. section 27.2 section 27.3.2

Forwarding can hide details of the original request. The app may see the proxy’s IP address instead of the client’s; if the proxy handles TLS, an original HTTPS request may arrive at the app as HTTP. A proxy can supply X-Forwarded-For and X-Forwarded-Proto headers so ForwardedHeadersMiddleware can update the request properties the app uses. section 27.3.2

Listing 27.1 Configuring an app to use forwarded headers in Startup.cs
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
WebApplication app = builder.Build();
 
app.UseForwardedHeaders(new ForwardedHeadersOptions         #1
{
    ForwardedHeaders = ForwardedHeaders.XForwardedFor |     #2
                       ForwardedHeaders.XForwardedProto     #2
});
app.UseHttpsRedirection();                                  #3
app.UseRouting();                                           #3
app.MapGet("/", () => "Hello world!");
app.Run();

The listing places UseForwardedHeaders before HTTPS redirection and configures it to read the forwarded client IP and protocol headers. section 27.3.2

For HTTPS, a proxy can pass encrypted traffic through to the app or terminate TLS itself. With TLS offloading, the browser-to-proxy connection uses HTTPS, but the proxy-to-app connection may be unencrypted. Account for that change when interpreting the request scheme. section 28.1 section 27.3.2

How ASP.NET Core in Action, 3e covers it

How deep it goes
Defines request forwarding and works through hosting benefits and forwarded headers, but stops short of exhaustive IIS or Linux proxy configuration.
Examples
Uses IIS deployment, NGINX and Apache alternatives, and a ForwardedHeadersMiddleware configuration listing.
What it assumes
Assumes familiarity with ASP.NET Core applications and HTTP requests while defining the reverse proxy itself.
What you can do after
Operate/configure it by choosing a proxy arrangement and configuring an ASP.NET Core app to interpret forwarded request headers.
How it connects
Connects Kestrel hosting to IIS, forwarded headers, HTTPS, and TLS offloading.

Also explained in

Compare how 5 books explain reverse proxy →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of ASP.NET Core in Action, 3e. The section links point to the parts of the book it draws on.