13

Basically, I have a couple of services. I want to forward every requests with prefix "/secured" to server1 port 80 and all other requests to server 2 port 80. The problem is that on server1, I am running service which accept the request without "/secured" prefix. In other words, I want to forward every requests such as "http://example.com/secured/api/getUser" to server1 as "http://example.com/api/getUser" (remove /secured from request' path).

With AWS ALB, currently the request is sent as http://example.com/secured/api/getUser; which forces me to update my server1's code so that the code handles requests with /secured prefix which doesn't look good.

Is there any easy way to solve this with ALB?

Thanks.

auxdx
  • 2,313
  • 3
  • 21
  • 25
  • 3
    The ALB isn't sophisticated enough to do this. You will have to use a reverse proxy for this. – Mark B Sep 04 '16 at 14:02

2 Answers2

6

I can confirm that this is unfortunately not possible with the ALB alone - and I agree it really should be.

AWS states:

Note that the path pattern is used to route requests but does not alter them. For example, if a rule has a path pattern of /img/*, the rule would forward a request for /img/picture.jpg to the specified target group as a request for /img/picture.jpg.

Kong
  • 8,792
  • 15
  • 68
  • 98
  • Things have moved on, this is a 5 year old question - you can do this through Route53 now - if you're a programmer you will need DevOps support though – Kong Oct 06 '21 at 10:15
  • Mr @kong, how can we achieve this using route53 ? – codeaprendiz Apr 17 '22 at 06:57
  • For anyone who stumbles upon this, I believe this is probably what @kong was referring to https://stackoverflow.com/questions/53157427/aws-elb-rewrite-path-and-alter-the-path-in-between – Campbell Mar 31 '23 at 15:09
4

I had the same issue, and as Mark pointed out, you can use reverse proxy on your server and do something like this (this is Nginx configuration):

server {
  listen 80 default_server;

  location /secured/ {
    proxy_pass http://localhost:{service_port}/;
  }
}

This will strip the /secured part and proxy everything else to your service. Just be sure to have the trailing / after the service port.

Zike
  • 49
  • 1
  • 2