Skip to content

Commit

Permalink
fix(gRPC): set :authority header to the upstream host (apache#7939)
Browse files Browse the repository at this point in the history
Signed-off-by: spacewander <spacewanderlzx@gmail.com>
  • Loading branch information
spacewander committed Nov 9, 2022
1 parent 063eae8 commit ee56792
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 0 deletions.
8 changes: 8 additions & 0 deletions apisix/cli/ngx_tpl.lua
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,14 @@ http {
apisix.grpc_access_phase()
}
{% if use_apisix_openresty then %}
# For servers which obey the standard, when `:authority` is missing,
# `host` will be used instead. When used with apisix-base, we can do
# better by setting `:authority` directly
grpc_set_header ":authority" $upstream_host;
{% else %}
grpc_set_header "Host" $upstream_host;
{% end %}
grpc_set_header Content-Type application/grpc;
grpc_socket_keepalive on;
grpc_pass $upstream_scheme://apisix_backend;
Expand Down
13 changes: 13 additions & 0 deletions t/APISIX.pm
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,19 @@ my $grpc_location = <<_EOC_;
apisix.grpc_access_phase()
}
_EOC_

if ($version =~ m/\/apisix-nginx-module/) {
$grpc_location .= <<_EOC_;
grpc_set_header ":authority" \$upstream_host;
_EOC_
} else {
$grpc_location .= <<_EOC_;
grpc_set_header "Host" \$upstream_host;
_EOC_
}

$grpc_location .= <<_EOC_;
grpc_set_header Content-Type application/grpc;
grpc_socket_keepalive on;
grpc_pass \$upstream_scheme://apisix_backend;
Expand Down
96 changes: 96 additions & 0 deletions t/node/grpc-proxy.t
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,99 @@ GET /hello
--- error_code: 502
--- error_log
upstream: "grpc://127.0.0.1:80"
=== TEST 7: set authority header
--- log_level: debug
--- http2
--- apisix_yaml
routes:
-
id: 1
uris:
- /helloworld.Greeter/SayHello
methods: [
POST
]
upstream:
scheme: grpc
nodes:
"127.0.0.1:50051": 1
type: roundrobin
#END
--- exec
grpcurl -import-path ./t/grpc_server_example/proto -proto helloworld.proto -plaintext -d '{"name":"apisix"}' 127.0.0.1:1984 helloworld.Greeter.SayHello
--- response_body
{
"message": "Hello apisix"
}
--- grep_error_log eval
qr/grpc header: "(:authority|host): [^"]+"/
--- grep_error_log_out eval
qr/grpc header: "(:authority|host): 127.0.0.1:1984"/
=== TEST 8: set authority header to node header
--- log_level: debug
--- http2
--- apisix_yaml
routes:
-
id: 1
uris:
- /helloworld.Greeter/SayHello
methods: [
POST
]
upstream:
scheme: grpc
pass_host: node
nodes:
"127.0.0.1:50051": 1
type: roundrobin
#END
--- exec
grpcurl -import-path ./t/grpc_server_example/proto -proto helloworld.proto -plaintext -d '{"name":"apisix"}' 127.0.0.1:1984 helloworld.Greeter.SayHello
--- response_body
{
"message": "Hello apisix"
}
--- grep_error_log eval
qr/grpc header: "(:authority|host): [^"]+"/
--- grep_error_log_out eval
qr/grpc header: "(:authority|host): 127.0.0.1:50051"/
=== TEST 9: set authority header to specific value
--- log_level: debug
--- http2
--- apisix_yaml
routes:
-
id: 1
uris:
- /helloworld.Greeter/SayHello
methods: [
POST
]
upstream:
scheme: grpc
pass_host: rewrite
upstream_host: hello.world
nodes:
"127.0.0.1:50051": 1
type: roundrobin
#END
--- exec
grpcurl -import-path ./t/grpc_server_example/proto -proto helloworld.proto -plaintext -d '{"name":"apisix"}' 127.0.0.1:1984 helloworld.Greeter.SayHello
--- response_body
{
"message": "Hello apisix"
}
--- grep_error_log eval
qr/grpc header: "(:authority|host): [^"]+"/
--- grep_error_log_out eval
qr/grpc header: "(:authority|host): hello.world"/

0 comments on commit ee56792

Please sign in to comment.