Skip to content

Instantly share code, notes, and snippets.

@rafi
Created August 10, 2020 09:47
Show Gist options
  • Save rafi/2c44971b76804b1398d1e82945dad9b7 to your computer and use it in GitHub Desktop.
Save rafi/2c44971b76804b1398d1e82945dad9b7 to your computer and use it in GitHub Desktop.
Kubernetes nginx-ingress routing by header value

During a Hackathon I implemented a route-interception by HTTP Header value in Nginx-ingress for Kubernetes:

Add Nginx-ingress annotation in global ConfigMap ('http' scope):

kind: ConfigMap
data:
  http-snippet: |
    map $http_x_feature_ucache $ucache_route_service {
      default "$proxy_upstream_name";
      'true' "default-cache-svc-3000";
    }

Add in original Ingress object the actual interception of upstream proxy ('location' scope):

  annotations:
    nginx.ingress.kubernetes.io/configuration-snippet: |
      set $proxy_upstream_name   $ucache_route_service;

This will cause the ingress to intercept requests with HTTP header X-Feature-UCache: true and re-route them to default-cache-svc-3000 (Namespace-Service-Port), thus, achieving a poor-man's content routing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment