我在开源最新的 nginx 上使用下面的 nginx conf:
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log notice;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
map $upstream_trailer_grpc_status $grpc_status {
default $upstream_trailer_grpc_status; # We normally expect to receive grpc-status as a trailer
'' $sent_http_grpc_status; # Else use the header, regardless of who generated it
}
log_format grpc_json escape=json '{"client":"$remote_addr",'
'"uri":"$uri","http-status":$status,'
'"grpc-status":$grpc_status,"upstream":"$upstream_addr",'
'"connection":$connection,"tx-bytes":$bytes_sent}';
upstream client_a_internal {
zone client_a_internal 64k;
server 127.0.0.1:7001;
server 127.0.0.1:7002;
}
server {
listen 7001 http2;
location / {
grpc_pass grpc://192.168.127.129:7001;
grpc_pass_header X-Upstream;
grpc_set_header X-Upstream 7001;
}
}
server {
listen 7002 http2;
location / {
grpc_pass grpc://192.168.127.129:7002;
grpc_pass_header X-Upstream;
grpc_set_header X-Upstream 7002;
}
}
server {
listen 5087 http2;
access_log /var/log/nginx/grpc_log.json grpc_json;
location / {
grpc_pass grpc://client_a_internal;
grpc_next_upstream error timeout http_500 http_502 http_503 http_504 non_idempotent;
}
}
}
此处 192.168.127.129:7002 始终处于关闭状态,而 192.168.127.129:7001 仅处于打开状态。我想检查 nginx 是否应该尝试其他服务器,以防出现 502 错误。
但我观察到它只尝试一个上游服务器,即使它得到 502 响应。
相同的访问日志:理想情况下,它应该在上游服务器中显示 2 个服务器,但事实并非如此。
{"client":"192.168.127.134","uri":"/helloworld.Greeter/SayHello","http-status":**502**,"grpc-status":,"upstream":"**127.0.0.1:7002**","connection":1,"tx-bytes":238}
{"client":"192.168.127.134","uri":"/helloworld.Greeter/SayHello","http-status":**502**,"grpc-status":,"upstream":"**127.0.0.1:7002**","connection":1,"tx-bytes":238}
{"client":"192.168.127.134","uri":"/helloworld.Greeter/SayHello","http-status":**502**,"grpc-status":,"upstream":"**127.0.0.1:7002**","connection":1,"tx-bytes":238}
{"client":"192.168.127.134","uri":"/helloworld.Greeter/SayHello","http-status":**502**,"grpc-status":,"upstream":"**127.0.0.1:7002**","connection":1,"tx-bytes":238}
{"client":"192.168.127.134","uri":"/helloworld.Greeter/SayHello","http-status":**502**,"grpc-status":,"upstream":"**127.0.0.1:7002**","connection":1,"tx-bytes":238}
{"client":"192.168.127.134","uri":"/helloworld.Greeter/SayHello","http-status":**200**,"grpc-status":0,"upstream":"127.0.0.1:7001","connection":1,"tx-bytes":183}
{"client":"192.168.127.134","uri":"/helloworld.Greeter/SayHello","http-status":**200**,"grpc-status":0,"upstream":"127.0.0.1:7001","connection":1,"tx-bytes":183}
{"client":"192.168.127.134","uri":"/helloworld.Greeter/SayHello","http-status":**200**,"grpc-status":0,"upstream":"127.0.0.1:7001","connection":1,"tx-bytes":183}
{"client":"192.168.127.134","uri":"/helloworld.Greeter/SayHello","http-status":**200**,"grpc-status":0,"upstream":"127.0.0.1:7001","connection":1,"tx-bytes":183}
{"client":"192.168.127.134","uri":"/helloworld.Greeter/SayHello","http-status":**200**,"grpc-status":0,"upstream":"127.0.0.1:7001","connection":1,"tx-bytes":183}