-
Notifications
You must be signed in to change notification settings - Fork 170
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
tkan145
merged 8 commits into
3scale:master
from
tkan145:THREESCALE-9542-chunked-request
Jan 22, 2024
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
3efa69a
Fix 'Transfer-Encoding: chunked' issue when sending request via proxy
tkan145 b02a888
Support chunked requests when talking to proxy servers with request b…
tkan145 e3f6db5
Fixed response body not being sent
tkan145 f5af618
Handle "Expect:100-continue" header only when raw socket is used
tkan145 5e05612
Update CHANGELOG.md file
tkan145 2bfc227
Wrapping block I/O call in a coroutine
tkan145 f19b9e5
Update request_unbuffered policy README.md file
tkan145 b494ca8
Address PR comments
tkan145 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,97 @@ | ||
# 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 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 the `request_unbuffered` 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 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. | ||
|
||
## Why does upstream receive a "Content-Length" header when the original request is sent with "Transfer-Encoding: chunked" | ||
|
||
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. 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 | ||
|
||
## 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:[email protected]: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 | ||
|
||
- 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. | ||
- 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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not true for the use case this PR is taking care of. Only when nginx handles the proxying task.
Actually, for a future enhancement, we could do the same. Read one buffer from the socket with size (S configurable). If all the full body has been read, send upstream with content-length. If the body full body is not read, proxy the request with TE: chunked.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated verification steps. I will address this is the future PR.