Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[THREESCALE-9542] Part 2: Add support to proxy request with Transfer-Encoding: chunked #1403

Merged
merged 8 commits into from
Jan 22, 2024
96 changes: 88 additions & 8 deletions gateway/src/apicast/policy/request_unbuffered/README.md
Original file line number Diff line number Diff line change
@@ -1,19 +1,99 @@
# APICast Request Unbuffered

This policy allows to disable request buffering
## Description

When enable this policy will dymanically sets the [`proxy_request_buffering: off`](https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_request_buffering
) directive per service.


## Technical details

By default, NGINX reads the entire request body into memory (or buffers large requests into disk) before proxying it to the upstream server. However, reading bodies can become expensive, especially when requests with large payloads are sent.
Copy link
Contributor

@dfennessy dfennessy Jan 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
By default, NGINX reads the entire request body into memory (or buffers large requests into disk) before proxying it to the upstream server. However, reading bodies can become expensive, especially when requests with large payloads are sent.
By default, NGINX reads the entire request body into memory or buffers large requests to disk before forwarding them to the upstream server. Reading bodies can become expensive, especially when sending requests containing large payloads.


For example, when the client sends 10GB, NGINX will buffer the entire 10GB to disk before sending anything to
the upstream server.

When `proxy_request_buffering` is in the chain, request buffering will be disabled and the request body will be sent to the proxied server immediately as it received. This can help minimize time spent sending data to a service and disk I/O for requests with big body. However, there are caveats and corner cases applied, [**Caveats**](#caveats)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

proxy_request_buffering is not the name of the policy, is it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oops

Copy link
Contributor

@dfennessy dfennessy Jan 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
When `proxy_request_buffering` is in the chain, request buffering will be disabled and the request body will be sent to the proxied server immediately as it received. This can help minimize time spent sending data to a service and disk I/O for requests with big body. However, there are caveats and corner cases applied, [**Caveats**](#caveats)
When the `proxy_request_buffering` is in the chain, request buffering is disabled, sending the request body to the proxied server immediately upon receiving it. This can help minimize time spent sending data to a service and disk I/O for requests with big body. However, there are caveats and corner cases applied, [**Caveats**](#caveats)


The policy also provides a consistent behavior across multiple scenarios like:

```
- APIcast <> upstream HTTP 1.1 plain
- APIcast <> upstream TLS
- APIcast <> HTTP Proxy (env var) <> upstream HTTP 1.1 plain
- APIcast <> HTTP Proxy (policy) <> upstream HTTP 1.1 plain
- APIcast <> HTTP Proxy (camel proxy) <> upstream HTTP 1.1 plain
- APIcast <> HTTP Proxy (env var) <> upstream TLS
- APIcast <> HTTP Proxy (policy) <> upstream TLS
- APIcast <> HTTP Proxy (camel proxy) <> upstream TLS
```

## Why don't we also support disable response buffering?

The response buffering is enabled by default in NGINX (the [`proxy_buffering: on`]() directive). It does
this to shield the backend against slow clients ([slowloris attack](https://en.wikipedia.org/wiki/Slowloris_(computer_security))).

If the `proxy_buffering` is disabled, the upstream server will be forced to keep the connection open until all data has been received by the client. Thereforce, NGINX [advises](https://www.nginx.com/blog/avoiding-top-10-nginx-configuration-mistakes/#proxy_buffering-off) against disabling `proxy_buffering` as it will potentially waste upstream server resources.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
If the `proxy_buffering` is disabled, the upstream server will be forced to keep the connection open until all data has been received by the client. Thereforce, NGINX [advises](https://www.nginx.com/blog/avoiding-top-10-nginx-configuration-mistakes/#proxy_buffering-off) against disabling `proxy_buffering` as it will potentially waste upstream server resources.
If the `proxy_buffering` is disabled, the upstream server keeps the connection open until all data is received by the client. NGINX [advises](https://www.nginx.com/blog/avoiding-top-10-nginx-configuration-mistakes/#proxy_buffering-off) against disabling `proxy_buffering` as it will potentially waste upstream server resources.


## Example configuration

```
{
"name": "request_unbuffered",
"version": "builtin",
"configuration": {}
}
"policy_chain": [
{
"name": "request_unbuffered",
"version": "builtin",
},
{
"name": "apicast.policy.apicast"
}
]
```

Use with Proxy policy

```
"policy_chain": [
{
"name": "request_unbuffered",
"version": "builtin",
},
{
"name": "apicast.policy.http_proxy",
"configuration": {
"all_proxy": "http://foo:bar@192.168.15.103:8888/",
"https_proxy": "http://192.168.15.103:8888/",
"http_proxy": "http://192.168.15.103:8888/"
}
}
]
```

Use with Camel Proxy policy

```
"policy_chain": [
{
"name": "request_unbuffered",
"version": "builtin",
},
{
"name": "apicast.policy.camel",
"configuration": {
"http_proxy": "http://192.168.15.103:8080/",
"https_proxy": "http://192.168.15.103:8443/",
"all_proxy": "http://192.168.15.103:8080/"
}
}
]
```

## Caveats

- Because APIcast allows defining mapping rules based on request content, POST requests with
`Content-type: application/x-www-form-urlencoded` will always be buffered regardless of the
- Because APIcast allows defining mapping rules based on request content, ie `POST /some_path?a_param={a_value}`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- Because APIcast allows defining mapping rules based on request content, ie `POST /some_path?a_param={a_value}`
- APIcast allows defining of mapping rules based on request content. For example, `POST /some_path?a_param={a_value}`

will match a request like `POST "http://apicast_host:8080/some_path"` with a form URL-encoded body like: `a_param=abc`
, requests with `Content-type: application/x-www-form-urlencoded` will always be buffered regardless of the
`request_unbuffered` policy is enabled or not.
- For a request with "small" body that fits into [`client_body_buffer_size`](https://nginx.org/en/docs/http/ngx_http_core_module.html#client_body_buffer_size) and with header "Transfer-Encoding: chunked", NGINX will always read and know the length of the body.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is nice to note, but it is not a caveat (a limitation), it is expected and correct. All about unbuffered request is the fact, as you correctly pointed out, that the request body will be sent to the proxied server immediately as it received. The transfer encoding is HOP-BY-HOP encoding and nothing prevents from changing it from one hop to the next one.

Then it will send the request to upstream with the "Content-Length" header.
- If a client uses chunked transfer encoding with HTTP/1.0, NGINX will always buffer the request body
- Disable request buffering could potentially expose the backend to [slowloris attack](https://en.wikipedia.org/wiki/Slowloris_(computer_security)). Therefore, we recommend to only use this policy when needed.
Loading