nginx icon indicating copy to clipboard operation
nginx copied to clipboard

Support set :authority pseudo-header field in proxy_v2/grpc request

Open HanadaLee opened this issue 6 months ago • 6 comments

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:

  1. A separate directive (proxy_set_authority and grpc_set_authority) is provided to set the authority, which acts as the host header in proxy http1 and as the :authority header in proxy v2/grpc.
  2. For forward compatibility, when client use proxy_set_header Host newhost.example.com;, it will be equivalent to proxy_set_authority newhost.example.com;. Furthermore, neither this directive nor proxy_set_header Host should be configured multiple times at the same level.

Alternatives Considered

No response

Additional Context

No response

HanadaLee avatar Jun 03 '26 13:06 HanadaLee

👋 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.

github-actions[bot] avatar Jun 03 '26 13:06 github-actions[bot]

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!

lizining1231 avatar Jun 13 '26 10:06 lizining1231

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.

HanadaLee avatar Jun 13 '26 11:06 HanadaLee

Hi @HanadaLee , I've opened a draft PR #1462 for this issue. Could you please take a look when you have time? Thanks!

lizining1231 avatar Jun 14 '26 19:06 lizining1231

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.

HanadaLee avatar Jun 16 '26 15:06 HanadaLee

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:

  1. Type change: Struct field ngx_str_t authority → ngx_http_complex_value_t *authority (for both proxy and grpc modules)
  2. Directive handler: ngx_conf_set_str_slot → ngx_http_set_complex_value_slot
  3. Initialization & inheritance: Added NGX_CONF_UNSET_PTR initialization in create_loc_conf, and switched to ngx_conf_merge_ptr_value macro in merge_loc_conf
  4. Host auto-sync logic: When proxy_set_header Host is set without a corresponding proxy_set_authority, the Host value is automatically compiled and synced to authority via ngx_http_compile_complex_value
  5. Runtime value resolution: Replaced with ngx_http_complex_value() in create_request to resolve the authority value, supporting runtime $variable resolution

Thanks again!

lizining1231 avatar Jun 19 '26 19:06 lizining1231