You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: aspnetcore/fundamentals/servers/yarp/diagnosing-yarp-issues.md
+68-50Lines changed: 68 additions & 50 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,17 +9,17 @@ ms.topic: article
9
9
content_well_notification: AI-contribution
10
10
ai-usage: ai-assisted
11
11
---
12
-
13
12
# YARP Diagnosing YARP-based proxies
14
13
15
14
When using a reverse proxy, there is an additional hop from the client to the proxy, and then from the proxy to destination for things to go wrong. This topic should provide some hints and tips for how to debug and diagnose issues when they occur. It assumes that the proxy is already running, and so does not include problems at startup such as configuration errors.
16
15
17
16
## Logging
17
+
18
18
The first step to being able to tell what is going on with YARP is to turn on [logging](/aspnet/core/fundamentals/logging/#configure-logging). This is a configuration flag so can be changed on the fly. YARP is implemented as a middleware component for ASP.NET Core, so you need to enable logging for both YARP and ASP.NET to get the complete picture of what is going on.
19
19
20
20
By default ASP.NET will log to the console, and the configuration file can be used to control the level of logging.
21
21
22
-
```Json
22
+
```json
23
23
//Sets the Logging level for ASP.NET
24
24
"Logging": {
25
25
"LogLevel": {
@@ -34,38 +34,39 @@ By default ASP.NET will log to the console, and the configuration file can be us
34
34
35
35
You want logging information from the `Microsoft.AspNetCore.\*` and `Yarp.ReverseProxy.\*` providers. The preceding example emits `Information`-level events from both providers to the console. Changing the level to `Debug` shows additional entries. ASP.NET implements change detection for configuration files, so you can edit the `appsettings.json` file (or `appsettings.development.json` for the `Development` environment) while the project is running and observe changes to the log output.
36
36
37
-
> Note: Settings in the `appsettings.development.jso`n file override settings in `appsettings.json` when running in the `Development` environment, so make sure that if you are editing `appsettings.json` that the values aren't overridden.
37
+
> [!NOTE]
38
+
> Settings in the `appsettings.development.json` file override settings in `appsettings.json` when running in the `Development` environment, so make sure that if you are editing `appsettings.json` that the values aren't overridden.
38
39
39
-
### Understanding Log entries
40
+
### Understanding log entries
40
41
41
42
The logging output is directly tied to the way that ASP.NET Core processes requests. It's important to realize that as middleware, YARP is relying on much of the ASP.NET functionality to process the requests, for example the following is for the processing of a request with "Debug" mode enabled:
42
43
43
44
| Level | Log Message | Description |
44
-
| -----| -----------|----------- |
45
+
| --- | --- | --- |
45
46
| dbug | Microsoft.AspNetCore.Server.Kestrel.Connections[39]<br>Connection id "0HMCD0JK7K51U" accepted.| Connections are independent of requests, so this is a new connection |
46
47
| dbug | Microsoft.AspNetCore.Server.Kestrel.Connections[1]<br>Connection id "0HMCD0JK7K51U" started. ||
47
48
| info | Microsoft.AspNetCore.Hosting.Diagnostics[1]<br>Request starting HTTP/1.1 GET http://localhost:5000/ - - | This is the incoming request to ASP.NET |
48
49
| dbug | Microsoft.AspNetCore.HostFiltering.HostFilteringMiddleware[0]<br>Wildcard detected, all requests with hosts will be allowed. | My configuration does not tie endpoints to specific hostnames |
49
50
| dbug | Microsoft.AspNetCore.Routing.Matching.DfaMatcher[1001]<br>1 candidate(s) found for the request path '/' | This shows what possible matches there are for the route |
50
-
| dbug | Microsoft.AspNetCore.Routing.Matching.DfaMatcher[1005]<br>Endpoint 'minimumroute' with route pattern '{\*\*catch-all}' is valid for the request path '/' | The minimum route from YARPs configuration has matched|
| info | Microsoft.AspNetCore.Routing.EndpointMiddleware[0]<br>Executing endpoint 'minimumroute' ||
53
-
| info | Yarp.ReverseProxy.Forwarder.HttpForwarder[9]<br>Proxying to http://www.example.com/| YARP is proxying the request to example.com |
54
-
| info | Microsoft.AspNetCore.Routing.EndpointMiddleware[1]<br>Executed endpoint 'minimumroute' ||
55
-
| dbug | Microsoft.AspNetCore.Server.Kestrel.Connections[9]<br>Connection id "0HMCD0JK7K51U" completed keep alive response. | The response has finished, but connection can be kept alive. |
51
+
| dbug | Microsoft.AspNetCore.Routing.Matching.DfaMatcher[1005]<br>Endpoint 'minimumroute' with route pattern '{\*\*catch-all}' is valid for the request path '/' | The minimum route from YARPs configuration has matched|
| info | Microsoft.AspNetCore.Routing.EndpointMiddleware[0]<br>Executing endpoint 'minimumroute' ||
54
+
| info | Yarp.ReverseProxy.Forwarder.HttpForwarder[9]<br>Proxying to http://www.example.com/| YARP is proxying the request to example.com |
55
+
| info | Microsoft.AspNetCore.Routing.EndpointMiddleware[1]<br>Executed endpoint 'minimumroute' ||
56
+
| dbug | Microsoft.AspNetCore.Server.Kestrel.Connections[9]<br>Connection id "0HMCD0JK7K51U" completed keep alive response. | The response has finished, but connection can be kept alive. |
56
57
| info | Microsoft.AspNetCore.Hosting.Diagnostics[2]<br>Request finished HTTP/1.1 GET http://localhost:5000/ - - - 200 1256 text/html;+charset=utf-8 12.7797ms| The response completed with status code 200, responding with 1256 bytes as text/html in ~13ms. |
57
-
| dbug | Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets[6]<br>Connection id "0HMCD0JK7K51U" received FIN. | Diagnostic information about the connection to determine who closed it and how cleanly |
58
-
| dbug | Microsoft.AspNetCore.Server.Kestrel.Connections[10]<br>Connection id "0HMCD0JK7K51U" disconnecting. ||
59
-
| dbug | Microsoft.AspNetCore.Server.Kestrel.Connections[2]<br>Connection id "0HMCD0JK7K51U" stopped. ||
60
-
| dbug | Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets[7]<br>Connection id "0HMCD0JK7K51U" sending FIN because: "The Socket transport's send loop completed gracefully." ||
58
+
| dbug | Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets[6]<br>Connection id "0HMCD0JK7K51U" received FIN. | Diagnostic information about the connection to determine who closed it and how cleanly |
59
+
| dbug | Microsoft.AspNetCore.Server.Kestrel.Connections[10]<br>Connection id "0HMCD0JK7K51U" disconnecting. ||
60
+
| dbug | Microsoft.AspNetCore.Server.Kestrel.Connections[2]<br>Connection id "0HMCD0JK7K51U" stopped. ||
61
+
| dbug | Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets[7]<br>Connection id "0HMCD0JK7K51U" sending FIN because: "The Socket transport's send loop completed gracefully." ||
61
62
62
63
The above gives general information about the request and how it was processed.
63
64
64
-
### Using ASP.NET Request Logging
65
+
### Using ASP.NET request logging
65
66
66
-
ASP.NET includes a middleware component that can be used to provide more details about the request and response. The `UseHttpLogging` component can be added to the request pipeline. It will add additional entries to the log detailing the incoming and outgoing request headers.
67
+
ASP.NET includes a middleware component that can be used to provide more details about the request and response. The `UseHttpLogging` component can be added to the request pipeline, which adds additional entries to the log detailing the incoming and outgoing request headers.
67
68
68
-
```C#
69
+
```csharp
69
70
app.UseHttpLogging();
70
71
// Enable endpoint routing, required for the reverse proxy
We recommend reading [Networking telemetry in .NET](/dotnet/fundamentals/networking/telemetry/overview) as a primer on how to consume networking telemetry in .NET.
108
109
@@ -111,58 +112,70 @@ The [Metrics sample](https://github.com/microsoft/reverse-proxy/tree/main/sample
111
112
*`ForwarderTelemetryConsumer`
112
113
*`HttpClientTelemetryConsumer`
113
114
114
-
To use either of these you create a class implementing a <!-- fix link -->`[telemetry interface](https://microsoft.github.io/reverse-proxy/api/Yarp.Telemetry.Consumption.html#interfaces)`, such as [`IForwarderTelemetryConsumer`](https://github.com/microsoft/reverse-proxy/blob/release/latest/src/TelemetryConsumption/Forwarder/IForwarderTelemetryConsumer.cs):
115
+
To use either of these, create a class implementing a [`Yarp.Telemetry.Consumption` interface](xref:Yarp.Telemetry.Consumption#interfaces), such as <xref:Yarp.Telemetry.Consumption.IForwarderTelemetryConsumer>:
The events for Telemetry are fired as they occur, so you can [fish out the HttpContext](/aspnet/core/fundamentals/http-context#use-httpcontext-from-custom-components) and the YARP feature from it:
201
+
The events for telemetry are fired as they occur, so you can [obtain the `HttpContext`](/aspnet/core/fundamentals/http-context#use-httpcontext-from-custom-components) and the YARP feature from it:
Another way to inspect the state for requests is to insert additional middleware into the request pipeline. You can insert between the other stages to see the state of the request.
206
223
207
-
```C#
224
+
```csharp
208
225
// We can customize the proxy pipeline and add/remove/replace steps
209
226
app.MapReverseProxy(proxyPipeline=>
210
227
{
@@ -234,18 +251,19 @@ public Task MyCustomProxyStep(HttpContext context, Func<Task> next)
234
251
235
252
You can also use [ASP.NET middleware](/aspnet/core/fundamentals/middleware/write) within Configure that will enable you to inspect the request before the proxy pipeline.
236
253
237
-
> **Note:** The proxy will stream the response from the destination server back to the client, so the response headers and body are not readily accessible via middleware.
254
+
> [!NOTE]
255
+
> The proxy streams the response from the destination server back to the client, so the response headers and body aren't readily accessible via middleware.
238
256
239
257
## Using the debugger
240
258
241
-
A debugger, such as Visual Studio can be attached to the proxy process. However, unless you have existing middleware, there is not a good place in the app code to break and inspect the state of the request. Therefore the debugger is best used in conjunction with one of the techniques above so that you have distinct places to insert breakpoints etc.
259
+
A debugger, such as Visual Studio, can be attached to the proxy process. However, unless you have existing middleware, there isn't a good place in the app code to break and inspect the state of the request. Therefore, the debugger is best used in conjunction with one of the preceding techniques so that you have distinct places to insert breakpoints.
242
260
243
-
## Network Tracing
261
+
## Network tracing
244
262
245
-
It can be attractive to use network tracing tools like [Fiddler](https://www.telerik.com/fiddler) or [Wireshark](https://www.wireshark.org/) to try to monitor what is happening either side of the proxy. However there are some gotchas to using both tools.
263
+
It can be attractive to use network tracing tools like [Fiddler](https://www.telerik.com/fiddler) or [Wireshark](https://www.wireshark.org/) to try to monitor what's happening either side of the proxy. However, be cautious using both tools:
246
264
247
-
- Fiddler registers itself as a proxy and relies on applications using the default proxy to be able to monitor traffic. This works for inbound traffic from a browser to YARP, but will not capture the outbound requests as YARP is configured to not use the proxy settings for outbound traffic.
248
-
- On windows Wireshark uses Npcap to capture packet data for network traffic, so it will capture both inbound and outbound traffic, and can be used to monitor HTTP traffic. Wireshark works out of the box for HTTP traffic.
249
-
- HTTPS traffic is encrypted, and so is not automatically decryptable by network monitoring tools. Each tool has workarounds that may enable traffic to be monitored, but they require hacking around with certificates and trust relationships. Because YARP is making outbound requests, techniques for tricking browsers do not apply to the YARP process.
265
+
* Fiddler registers itself as a proxy and relies on apps using the default proxy to be able to monitor traffic. This works for inbound traffic from a browser to YARP but won't capture the outbound requests, as YARP is configured to not use the proxy settings for outbound traffic.
266
+
* On Windows, Wireshark uses Npcap to capture packet data for network traffic, so it captures both inbound and outbound traffic and can be used to monitor HTTP traffic.
267
+
* HTTPS traffic is encrypted and isn't automatically decryptable by network monitoring tools. Each tool has workarounds that may enable traffic to be monitored, but they require risky use of certificates and changes to trust relationships. Because YARP is making outbound requests, techniques for tricking browsers don't apply to the YARP process.
250
268
251
-
The protocol choice for outbound traffic is made based on the destination URL in the cluster configuration. If traffic monitoring is being used for diagnostics then, if possible, changing the outbound URLs to "http://" may be simplest approach to enable the monitoring tools to work, provided the issues being diagnosed are not transport protocol related.
269
+
The protocol choice for outbound traffic is made based on the destination URL in the cluster configuration. If traffic monitoring is used for diagnostics, changing the outbound URLs to `http://`, if possible, may be the simplest approach to enable the monitoring tools to work, provided the issues being diagnosed aren't transport protocol related.
0 commit comments