Support set :authority pseudo-header field in proxy_v2/grpc request
Feature Overview
Currently, nginx by default will pass the following ":authority" pseudo-header upstream when using proxy_pass over http2 or gRPC_pass. However, you'll encounter problems when you want to specify :authority. Currently, the only solution is proxy_set_header Host newhost.example.com; However, at this point, neither the proxy_v2 nor the gRPC module will send the ":authority" pseudo-header. Instead, they will send the Host header.
This shouldn't be a problem in typical use cases. This is because RFC 9113 states: An intermediary that forwards a request over HTTP/2 will retain any Host header field. However, many upstream services do not recognize such requests, and sending an H2 or H3 request with only a HOST header may cause them to refuse to respond.
To address this issue, apisix currently has a patch that allows configuring :authority. via grpc_set_header(grpc_set_header :authority newhost.example.com;), but we may need a more general solution.
Suggestions:
- A separate directive (proxy_set_authority and grpc_set_authority) is provided to set the authority, which acts as the
hostheader in proxy http1 and as the:authorityheader in proxy v2/grpc. - For forward compatibility, when client use
proxy_set_header Host newhost.example.com;, it will be equivalent toproxy_set_authority newhost.example.com;. Furthermore, neither this directive norproxy_set_header Hostshould be configured multiple times at the same level.
Alternatives Considered
No response
Additional Context
No response
👋 Thanks for opening this issue and contributing to the NGINX project! A maintainer will review it and follow up when possible. We appreciate your support.
Hi! I'm interested in working on this issue.
Following the suggestion, I've started implementing:
-
proxy_set_authority directive that acts as the :authority header in proxy_v2/grpc
-
grpc_set_authority directive for the gRPC module
-
Fixed the bug where proxy_set_header Host causes :authority to be omitted
After implementation, would it be OK if I open a draft PR for discussion?
Thanks!
Hi! I'm interested in working on this issue.
Following the suggestion, I've started implementing:
- proxy_set_authority directive that acts as the :authority header in proxy_v2/grpc
- grpc_set_authority directive for the gRPC module
- Fixed the bug where proxy_set_header Host causes :authority to be omitted
After implementation, would it be OK if I open a draft PR for discussion?
Thanks!
It looks good.
Hi @HanadaLee , I've opened a draft PR #1462 for this issue. Could you please take a look when you have time? Thanks!
Hi @HanadaLee , I've opened a draft PR #1462 for this issue. Could you please take a look when you have time? Thanks!
I have conducted a preliminary review of the code. I found that you are setting the value of the newly introduced directives to a string and not supporting variables, which will break the existing implementation. We need to support variables.
I have conducted a preliminary review of the code. I found that you are setting the value of the newly introduced directives to a string and not supporting variables, which will break the existing implementation. We need to support variables.
Thank you very much for your review! Based on the feedback, I have changed proxy_set_authority and grpc_set_authority from ngx_str_t to ngx_http_complex_value_t * to support variables.
The specific changes are as follows:
- Type change: Struct field
ngx_str_t authority→ngx_http_complex_value_t *authority(for both proxy and grpc modules) - Directive handler:
ngx_conf_set_str_slot→ngx_http_set_complex_value_slot - Initialization & inheritance: Added
NGX_CONF_UNSET_PTRinitialization increate_loc_conf, and switched tongx_conf_merge_ptr_valuemacro inmerge_loc_conf - Host auto-sync logic: When
proxy_set_header Hostis set without a correspondingproxy_set_authority, the Host value is automatically compiled and synced to authority viangx_http_compile_complex_value - Runtime value resolution: Replaced with
ngx_http_complex_value()increate_requestto resolve the authority value, supporting runtime$variableresolution
Thanks again!