'Envoy as proxy to public internet services

How can I configure Envoy as a proxy to public internet services, such that each path might route to a different service? For example:

http://foo.example.com/service1/* -> http://somewhere.com/*
http://foo.example.com/service2/* -> http://somewhereelse.com/*


Solution 1:[1]

Assuming your envoy proxy is accessible at http://foo.example.com, you can use a config like this :

static_resources:
  listeners:
  - address:
      socket_address:
        address: 0.0.0.0
        port_value: 80
    filter_chains:
    - filters:
      - name: envoy.filters.network.http_connection_manager
        typed_config:
          "@type": type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager
          codec_type: AUTO
          stat_prefix: ingress_http
          route_config:
            name: local_route
            virtual_hosts:
            - name: backend
              domains:
              - "*"
              routes:
              - match:
                  prefix: "/service1"
                route:
                  prefix_rewrite: "/"
                  cluster: service1
                  auto_host_rewrite: true
              - match:
                  prefix: "/service2"
                route:
                  prefix_rewrite: "/"
                  cluster: service2
                  auto_host_rewrite: true
          http_filters:
          - name: envoy.filters.http.router
  clusters:
  - name: service1
    connect_timeout: 15s
    type: LOGICAL_DNS
    lb_policy: ROUND_ROBIN
    load_assignment:
      cluster_name: service1
      endpoints:
      - lb_endpoints:
        - endpoint:
            address:
              socket_address:
                address: somewhere.com
                port_value: 80
  - name: service2
    connect_timeout: 15s
    type: LOGICAL_DNS
    lb_policy: ROUND_ROBIN
    load_assignment:
      cluster_name: service2
      endpoints:
      - lb_endpoints:
        - endpoint:
            address:
              socket_address:
                address: somewhereelse.com
                port_value: 80

The important parts are :

  • prefix_rewrite: "/" to rewrite http://foo.example.com/service1/stuff to http://somewhere.com/stuff (in fact, to remove /service1)
  • auto_host_rewrite: true to add a Host header during forwarding (hostname of the upstream host of the cluster)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 norbjd