Search code examples
amazon-web-servicesamazon-elbaws-nlb

AWS NLB: forwarding request to different ports of a single host based on Path


with this flow:

external world --> AWS API Gateway ---> VPC Link ---> Network Load Balancer ---> my single EC2 instance

How can I configure AWS Netword Load Balancer such that:

  1. Requests to https://myapp.com is routed into port 80 of my EC2 instance.

  2. Requests to https://myapp.com/api/* is routed into port 3000 of my EC2 instance.

?

Currently I have only configured one Listener on the NLB that listens on port 80 and all traffics from the API Gateway are routed to port 80 of my EC2 instance.

I have found that in Application Load Balancer, you can configure "Rules" that map path to different ports: Path based routing in AWS ALB to single host with multiple ports

Is this available with NLB?


Solution

  • This is not possible with the Network Load Balancer, because it operates on a level of the network stack that has no concept of Paths.

    The NLB operates on Layer 4 and supports the protocols TCP and UDP. These essentially create a connection between ports on two machines that allow data to flow between them.

    Paths as in HTTP(S) Paths are a Layer 5+ concept and belong to the HTTP Protocol. They're not available to the NLB because it can only work based on data that's guaranteed to be available there.

    You can use an Application Load Balancer as the target for your Network Load Balancer and then configure the Path-based rules there, because the ALB is a layer 5+ load balancer and understands the Layer 5 protocol HTTP.

    Here is a blog detailing this: Application Load Balancer-type Target Group for Network Load Balancer