Vert.x Http Proxy

Vert.x Http Proxy is a reverse proxy based on Vert.x, it aims to implement reusable reverse proxy logic to focus on higher concerns.

Warning
This module has Tech Preview status, this means the API can change between versions.

Using Vert.x Http Proxy

To use Vert.x Http Proxy, add the following dependency to the dependencies section of your build descriptor:

  • Maven (in your pom.xml):

<dependency>
 <groupId>io.vertx</groupId>
 <artifactId>vertx-http-proxy</artifactId>
 <version>4.1.8</version>
</dependency>
  • Gradle (in your build.gradle file):

dependencies {
 compile 'io.vertx:vertx-http-proxy:4.1.8'
}

Basic Http Proxy

In order to accomplish a reverse proxy with Vert.x Http Proxy you need the following:

  1. Proxy Server that handles outbound requests and forward them to the origin server using HttpProxy instance.

  2. Origin Server that handles requests from the proxy server and handles responses accordingly.

Now, you have the overall concept so let’s dive in implementation and begin with origin server then the proxy server with HttpProxy:

Origin Server

You simply create the origin server that listens to port 7070

HttpServer originServer = vertx.createHttpServer();

originServer.requestHandler(req -> {
  req.response()
    .putHeader("content-type", "text/html")
    .end("<html><body><h1>I'm the target resource!</h1></body></html>");
}).listen(7070);

Proxy Server With HttpProxy

Create the proxy server that listens to port 8080 with HttpProxy instance that handles reverse proxy logic accordingly.

HttpClient proxyClient = vertx.createHttpClient();

HttpProxy proxy = HttpProxy.reverseProxy(proxyClient);
proxy.origin(7070, "origin");

HttpServer proxyServer = vertx.createHttpServer();

proxyServer.requestHandler(proxy).listen(8080);

Finally, all outbound requests will be forwarded as a reverse proxy to the origin server conveniently.