Skip to content

Instantly share code, notes, and snippets.

@nginx-gists
Last active November 10, 2022 23:54
Show Gist options
  • Save nginx-gists/87ed942d4ee9f7e7ebb2ccf757ed90be to your computer and use it in GitHub Desktop.
Save nginx-gists/87ed942d4ee9f7e7ebb2ccf757ed90be to your computer and use it in GitHub Desktop.

Revisions

  1. nginx-gists revised this gist Oct 12, 2021. 2 changed files with 12 additions and 12 deletions.
    12 changes: 6 additions & 6 deletions grpc_gateway.conf
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,11 @@
    log_format grpc_json escape=json '{"timestamp":"$time_iso8601","client":"$remote_addr",'
    '"uri":"$uri","http-status":$status,'
    '"grpc-status":$grpc_status,"upstream":"$upstream_addr"'
    '"rx-bytes":$request_length,"tx-bytes":$bytes_sent}';
    log_format grpc_json escape=json '{"timestamp":"$time_iso8601",'
    '"client":"$remote_addr","uri":"$uri","http-status":$status,'
    '"grpc-status":$grpc_status,"upstream":"$upstream_addr"'
    '"rx-bytes":$request_length,"tx-bytes":$bytes_sent}';

    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
    default $upstream_trailer_grpc_status; # grpc-status is usually a trailer
    '' $sent_http_grpc_status; # Else use the header, whatever its source
    }

    server {
    12 changes: 6 additions & 6 deletions grpc_health_check.conf
    Original file line number Diff line number Diff line change
    @@ -1,11 +1,11 @@
    log_format grpc_json escape=json '{"timestamp":"$time_iso8601","client":"$remote_addr",'
    '"uri":"$uri","http-status":$status,'
    '"grpc-status":$grpc_status,"upstream":"$upstream_addr"'
    '"rx-bytes":$request_length,"tx-bytes":$bytes_sent}';
    log_format grpc_json escape=json '{"timestamp":"$time_iso8601",'
    '"client":"$remote_addr","uri":"$uri","http-status":$status,'
    '"grpc-status":$grpc_status,"upstream":"$upstream_addr"'
    '"rx-bytes":$request_length,"tx-bytes":$bytes_sent}';

    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
    default $upstream_trailer_grpc_status; # grpc-status is usually a trailer
    '' $sent_http_grpc_status; # Else use the header, whatever its source
    }

    server {
  2. nginx-gists revised this gist Jan 21, 2021. 1 changed file with 0 additions and 40 deletions.
    40 changes: 0 additions & 40 deletions grpc_health_checkNEW.conf
    Original file line number Diff line number Diff line change
    @@ -1,40 +0,0 @@
    log_format grpc_json escape=json '{"timestamp":"$time_iso8601","client":"$remote_addr",'
    '"uri":"$uri","http-status":$status,'
    '"grpc-status":$grpc_status,"upstream":"$upstream_addr"'
    '"rx-bytes":$request_length,"tx-bytes":$bytes_sent}';

    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
    }

    server {
    listen 50051 http2; # Plaintext

    # Routing
    location /routeguide. {
    grpc_pass grpc://routeguide_service;
    health_check type=grpc grpc_status=12; # 12=unimplemented
    }
    location /helloworld. {
    grpc_pass grpc://helloworld_service;
    health_check type=grpc grpc_status=12; # 12=unimplemented
    }
    }

    # Backend gRPC servers
    #
    upstream routeguide_service {
    zone routeguide_service 64k;
    server 127.0.0.1:10001;
    server 127.0.0.1:10002;
    server 127.0.0.1:10003;
    }

    upstream helloworld_service {
    zone helloworld_service 64k;
    server 127.0.0.1:20001;
    server 127.0.0.1:20002;
    }

    # vim: syntax=nginx
  3. nginx-gists revised this gist Jan 21, 2021. 1 changed file with 23 additions and 14 deletions.
    37 changes: 23 additions & 14 deletions grpc_health_check.conf
    Original file line number Diff line number Diff line change
    @@ -1,26 +1,35 @@
    log_format grpc_json escape=json '{"timestamp":"$time_iso8601","client":"$remote_addr",'
    '"uri":"$uri","http-status":$status,'
    '"grpc-status":$grpc_status,"upstream":"$upstream_addr"'
    '"rx-bytes":$request_length,"tx-bytes":$bytes_sent}';

    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
    }

    server {
    listen 50051 http2; # Plaintext

    # Routing
    location /helloworld. {
    grpc_pass grpc://helloworld_service;
    location /routeguide. {
    grpc_pass grpc://routeguide_service;
    health_check type=grpc grpc_status=12; # 12=unimplemented
    }

    # Health-check the helloworld containers
    location @helloworld_health {
    health_check mandatory uri=/nginx.health/check match=grpc_unknown;
    grpc_set_header Content-Type application/grpc;
    grpc_set_header TE trailers;
    location /helloworld. {
    grpc_pass grpc://helloworld_service;
    health_check type=grpc grpc_status=12; # 12=unimplemented
    }
    }

    # Specify the expected response to the health check (this
    # assumes that the gRPC service responds to GET requests)
    match grpc_unknown {
    header Content-Type = application/grpc;
    header grpc-status = 12; # unimplemented / unknown method
    }
    # Backend gRPC servers
    #
    upstream routeguide_service {
    zone routeguide_service 64k;
    server 127.0.0.1:10001;
    server 127.0.0.1:10002;
    server 127.0.0.1:10003;
    }

    upstream helloworld_service {
    zone helloworld_service 64k;
  4. nginx-gists revised this gist Jan 21, 2021. 1 changed file with 2 additions and 4 deletions.
    6 changes: 2 additions & 4 deletions grpc_gateway.conf
    Original file line number Diff line number Diff line change
    @@ -4,10 +4,8 @@ log_format grpc_json escape=json '{"timestamp":"$time_iso8601","client":"$remote
    '"rx-bytes":$request_length,"tx-bytes":$bytes_sent}';

    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
    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
    }

    server {
  5. nginx-gists revised this gist Jan 21, 2021. 1 changed file with 40 additions and 0 deletions.
    40 changes: 40 additions & 0 deletions grpc_health_checkNEW.conf
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,40 @@
    log_format grpc_json escape=json '{"timestamp":"$time_iso8601","client":"$remote_addr",'
    '"uri":"$uri","http-status":$status,'
    '"grpc-status":$grpc_status,"upstream":"$upstream_addr"'
    '"rx-bytes":$request_length,"tx-bytes":$bytes_sent}';

    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
    }

    server {
    listen 50051 http2; # Plaintext

    # Routing
    location /routeguide. {
    grpc_pass grpc://routeguide_service;
    health_check type=grpc grpc_status=12; # 12=unimplemented
    }
    location /helloworld. {
    grpc_pass grpc://helloworld_service;
    health_check type=grpc grpc_status=12; # 12=unimplemented
    }
    }

    # Backend gRPC servers
    #
    upstream routeguide_service {
    zone routeguide_service 64k;
    server 127.0.0.1:10001;
    server 127.0.0.1:10002;
    server 127.0.0.1:10003;
    }

    upstream helloworld_service {
    zone helloworld_service 64k;
    server 127.0.0.1:20001;
    server 127.0.0.1:20002;
    }

    # vim: syntax=nginx
  6. nginx-gists revised this gist Nov 21, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion grpc_health_check.conf
    Original file line number Diff line number Diff line change
    @@ -10,7 +10,7 @@ server {
    location @helloworld_health {
    health_check mandatory uri=/nginx.health/check match=grpc_unknown;
    grpc_set_header Content-Type application/grpc;
    grpc_set_header TE Trailers;
    grpc_set_header TE trailers;
    grpc_pass grpc://helloworld_service;
    }
    }
  7. nginx-gists revised this gist Aug 23, 2018. 1 changed file with 8 additions and 2 deletions.
    10 changes: 8 additions & 2 deletions grpc_health_check.conf
    Original file line number Diff line number Diff line change
    @@ -15,11 +15,17 @@ server {
    }
    }

    # Specify the expected response from the health_check directive
    # (it assumes that the gRPC service responds to GET requests)
    # Specify the expected response to the health check (this
    # assumes that the gRPC service responds to GET requests)
    match grpc_unknown {
    header Content-Type = application/grpc;
    header grpc-status = 12; # unimplemented / unknown method
    }

    upstream helloworld_service {
    zone helloworld_service 64k;
    server 127.0.0.1:20001;
    server 127.0.0.1:20002;
    }

    # vim: syntax=nginx
  8. nginx-gists revised this gist Aug 23, 2018. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions grpc_health_check.conf
    Original file line number Diff line number Diff line change
    @@ -15,8 +15,8 @@ server {
    }
    }

    # This is the expected response from the health_check directive requires that
    # the gRPC service responds to GET requests)
    # Specify the expected response from the health_check directive
    # (it assumes that the gRPC service responds to GET requests)
    match grpc_unknown {
    header Content-Type = application/grpc;
    header grpc-status = 12; # unimplemented / unknown method
  9. nginx-gists revised this gist Aug 23, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion grpc_gateway.conf
    Original file line number Diff line number Diff line change
    @@ -11,7 +11,7 @@ map $upstream_trailer_grpc_status $grpc_status {
    }

    server {
    listen 50051 http2; # Comment this line in production to disable plaintext port
    listen 50051 http2; # In production, comment out to disable plaintext port
    listen 443 http2 ssl;
    server_name grpc.example.com;
    access_log /var/log/nginx/grpc_log.json grpc_json;
  10. nginx-gists revised this gist Aug 23, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion grpc_gateway.conf
    Original file line number Diff line number Diff line change
    @@ -11,7 +11,7 @@ map $upstream_trailer_grpc_status $grpc_status {
    }

    server {
    listen 50051 http2; # Remove plaintext port for production use
    listen 50051 http2; # Comment this line in production to disable plaintext port
    listen 443 http2 ssl;
    server_name grpc.example.com;
    access_log /var/log/nginx/grpc_log.json grpc_json;
  11. nginx-gists revised this gist Aug 23, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion grpc_health_check.conf
    Original file line number Diff line number Diff line change
    @@ -14,7 +14,7 @@ server {
    grpc_pass grpc://helloworld_service;
    }
    }
    12345678901234567890123456789012345678901234567890123456789012345678901234567890

    # This is the expected response from the health_check directive requires that
    # the gRPC service responds to GET requests)
    match grpc_unknown {
  12. nginx-gists revised this gist Aug 23, 2018. 2 changed files with 7 additions and 5 deletions.
    6 changes: 4 additions & 2 deletions grpc_gateway.conf
    Original file line number Diff line number Diff line change
    @@ -4,8 +4,10 @@ log_format grpc_json escape=json '{"timestamp":"$time_iso8601","client":"$remote
    '"rx-bytes":$request_length,"tx-bytes":$bytes_sent}';

    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
    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
    }

    server {
    6 changes: 3 additions & 3 deletions grpc_health_check.conf
    Original file line number Diff line number Diff line change
    @@ -14,9 +14,9 @@ server {
    grpc_pass grpc://helloworld_service;
    }
    }

    # This is the expected response from the health_check directive
    # (requires that the gRPC service responds to GET requests)
    12345678901234567890123456789012345678901234567890123456789012345678901234567890
    # This is the expected response from the health_check directive requires that
    # the gRPC service responds to GET requests)
    match grpc_unknown {
    header Content-Type = application/grpc;
    header grpc-status = 12; # unimplemented / unknown method
  13. nginx-gists revised this gist Aug 23, 2018. No changes.
  14. nginx-gists revised this gist Aug 23, 2018. 6 changed files with 135 additions and 0 deletions.
    73 changes: 73 additions & 0 deletions errors.grpc_conf
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,73 @@
    # Standard HTTP-to-gRPC status code mappings
    # Ref: https://github.com/grpc/grpc/blob/master/doc/http-grpc-status-mapping.md
    #
    error_page 400 = @grpc_internal;
    error_page 401 = @grpc_unauthenticated;
    error_page 403 = @grpc_permission_denied;
    error_page 404 = @grpc_unimplemented;
    error_page 429 = @grpc_unavailable;
    error_page 502 = @grpc_unavailable;
    error_page 503 = @grpc_unavailable;
    error_page 504 = @grpc_unavailable;

    # NGINX-to-gRPC status code mappings
    # Ref: https://github.com/grpc/grpc/blob/master/doc/statuscodes.md
    #
    error_page 405 = @grpc_internal; # Method not allowed
    error_page 408 = @grpc_deadline_exceeded; # Request timeout
    error_page 413 = @grpc_resource_exhausted; # Payload too large
    error_page 414 = @grpc_resource_exhausted; # Request URI too large
    error_page 415 = @grpc_internal; # Unsupported media type;
    error_page 426 = @grpc_internal; # HTTP request was sent to HTTPS port
    error_page 495 = @grpc_unauthenticated; # Client certificate authentication error
    error_page 496 = @grpc_unauthenticated; # Client certificate not presented
    error_page 497 = @grpc_internal; # HTTP request was sent to mutual TLS port
    error_page 500 = @grpc_internal; # Server error
    error_page 501 = @grpc_internal; # Not implemented

    # gRPC error responses
    # Ref: https://github.com/grpc/grpc-go/blob/master/codes/codes.go
    #
    location @grpc_deadline_exceeded {
    add_header grpc-status 4;
    add_header grpc-message 'deadline exceeded';
    return 204;
    }

    location @grpc_permission_denied {
    add_header grpc-status 7;
    add_header grpc-message 'permission denied';
    return 204;
    }

    location @grpc_resource_exhausted {
    add_header grpc-status 8;
    add_header grpc-message 'resource exhausted';
    return 204;
    }

    location @grpc_unimplemented {
    add_header grpc-status 12;
    add_header grpc-message unimplemented;
    return 204;
    }

    location @grpc_internal {
    add_header grpc-status 13;
    add_header grpc-message 'internal error';
    return 204;
    }

    location @grpc_unavailable {
    add_header grpc-status 14;
    add_header grpc-message unavailable;
    return 204;
    }

    location @grpc_unauthenticated {
    add_header grpc-status 16;
    add_header grpc-message unauthenticated;
    return 204;
    }

    # vim: syntax=nginx
    7 changes: 7 additions & 0 deletions grpc_auth_jwt.conf
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    location /routeguide. {
    auth_jwt realm=routeguide token=$http_auth_token;
    auth_jwt_key_file my_idp.jwk;
    grpc_pass grpc://routeguide_service;
    }

    # vim: syntax=nginx
    25 changes: 25 additions & 0 deletions grpc_health_check.conf
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    server {
    listen 50051 http2; # Plaintext

    # Routing
    location /helloworld. {
    grpc_pass grpc://helloworld_service;
    }

    # Health-check the helloworld containers
    location @helloworld_health {
    health_check mandatory uri=/nginx.health/check match=grpc_unknown;
    grpc_set_header Content-Type application/grpc;
    grpc_set_header TE Trailers;
    grpc_pass grpc://helloworld_service;
    }
    }

    # This is the expected response from the health_check directive
    # (requires that the gRPC service responds to GET requests)
    match grpc_unknown {
    header Content-Type = application/grpc;
    header grpc-status = 12; # unimplemented / unknown method
    }

    # vim: syntax=nginx
    11 changes: 11 additions & 0 deletions grpc_precise_routing.conf
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    # Service-level routing
    location /routeguide.RouteGuide/ {
    grpc_pass grpc://routeguide_service_default;
    }

    # Method-level routing
    location = /routeguide.RouteGuide/RouteChat {
    grpc_pass grpc://routeguide_service_streaming;
    }

    # vim: syntax=nginx
    9 changes: 9 additions & 0 deletions helloworld.Dockerfile
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    # This Dockerfile runs the helloworld server from
    # https://grpc.io/docs/quickstart/go.html

    FROM golang
    RUN go get -u google.golang.org/grpc
    WORKDIR $GOPATH/src/google.golang.org/grpc/examples/helloworld

    EXPOSE 50051
    CMD ["go", "run", "greeter_server/main.go"]
    10 changes: 10 additions & 0 deletions routeguide.Dockerfile
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    # This Dockerfile runs the RouteGuide server from
    # https://grpc.io/docs/tutorials/basic/python.html

    FROM python
    RUN pip install grpcio-tools
    RUN git clone -b v1.14.x https://github.com/grpc/grpc
    WORKDIR grpc/examples/python/route_guide

    EXPOSE 50051
    CMD ["python", "route_guide_server.py"]
  15. nginx-gists revised this gist Aug 23, 2018. 1 changed file with 30 additions and 2 deletions.
    32 changes: 30 additions & 2 deletions grpc_gateway.conf
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@ log_format grpc_json escape=json '{"timestamp":"$time_iso8601","client":"$remote

    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 send the header, regardless of who generated it
    '' $sent_http_grpc_status; # Else use the header, regardless of who generated it
    }

    server {
    @@ -21,5 +21,33 @@ server {
    ssl_session_timeout 5m;
    ssl_ciphers HIGH:!aNULL:!MD5;
    ssl_protocols TLSv1.2 TLSv1.3;


    # Routing
    location /routeguide. {
    grpc_pass grpc://routeguide_service;
    }
    location /helloworld. {
    grpc_pass grpc://helloworld_service;
    }

    # Error responses
    include conf.d/errors.grpc_conf; # gRPC-compliant error responses
    default_type application/grpc; # Ensure gRPC for all error responses
    }

    # Backend gRPC servers
    #
    upstream routeguide_service {
    zone routeguide_service 64k;
    server 127.0.0.1:10001;
    server 127.0.0.1:10002;
    server 127.0.0.1:10003;
    }

    upstream helloworld_service {
    zone helloworld_service 64k;
    server 127.0.0.1:20001;
    server 127.0.0.1:20002;
    }

    # vim: syntax=nginx
  16. nginx-gists created this gist Aug 23, 2018.
    25 changes: 25 additions & 0 deletions grpc_gateway.conf
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    log_format grpc_json escape=json '{"timestamp":"$time_iso8601","client":"$remote_addr",'
    '"uri":"$uri","http-status":$status,'
    '"grpc-status":$grpc_status,"upstream":"$upstream_addr"'
    '"rx-bytes":$request_length,"tx-bytes":$bytes_sent}';

    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 send the header, regardless of who generated it
    }

    server {
    listen 50051 http2; # Remove plaintext port for production use
    listen 443 http2 ssl;
    server_name grpc.example.com;
    access_log /var/log/nginx/grpc_log.json grpc_json;

    # TLS config
    ssl_certificate /etc/ssl/certs/grpc.example.com.crt;
    ssl_certificate_key /etc/ssl/private/grpc.example.com.key;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 5m;
    ssl_ciphers HIGH:!aNULL:!MD5;
    ssl_protocols TLSv1.2 TLSv1.3;

    # vim: syntax=nginx