Search code examples
reverse-proxyenvoyproxy

How to use Envoy as a Reverse Proxy for a specific URL on localhost


I want to use Envoy as a reverse proxy in which I want to redirect the request from

http://example.com:3443/node-exporter/metrics 

to

http://localhost:9100/metrics

I want to redirect to a specific URL /metrics on the port 9100.

This is my current envoy_conf.yaml file

 listeners:
 - name: prom_listener
   address:
     socket_address : {address: 0.0.0.0, port_value: 3443}
   filter_chains:
   - name: prom_filter_chain
     filters:
     - name: envoy.filters.network.http_connection_manager
       typed_config:
         "@type": type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager
         stat_prefix: http_connection_manager
         route_config:
           virtual_hosts:
           - name: prom_local_host
             domains: ["*"]
             routes:
             - name: node-exporter-route
               match: {prefix: "/node-exporter/"}
               route:
                 cluster: node-exporter-cluster-server
                 timeout: 0s
                 idle_timeout: 0s
         http_filters:
         - name: envoy.filters.http.router
           typed_config:
             "@type": type.googleapis.com/envoy.extensions.filters.http.router.v3.Router
 clusters:
 - name: node-exporter-cluster-server
   type: static
   connect_timeout: 2s
   load_assignment:
     cluster_name: node-exporter-cluster-server
     endpoints:
     - lb_endpoints:
       - endpoint:
           address:
             socket_address:
               address: 127.0.0.1
               port_value: 9100

What changes (additions/deletions) should I make in this in order to achieve the reverse proxying to a specific url on the localhost and port mentioned?


Solution

  • With your configuration, when you are requesting example.com:3443/node-exporter/metrics, you are actually trying to access 127.0.0.1:9100/node-exporter/metrics, which does not exist.

    To access 127.0.0.1:9100/metrics (notice the missing /node-exporter part of the URL), you only have to configure your route to tell Envoy to rewrite the prefix. You should use the prefix_rewrite option to strip the /node-exporter part:

    routes:
    - name: node-exporter-route
      match: {prefix: "/node-exporter/"}
      route:
        cluster: node-exporter-cluster-server
        prefix_rewrite: "/"
        timeout: 0s
        idle_timeout: 0s