简体   繁体   中英

node load balancer and path based routing

I have a web application that I have hosted on AWS. This service works fine when I don't have an ALB.

The problem I face is the fact that I use path based routing, but don't want to modify my application code just because of the load balancer.

Internally the app sits on somewhere like here and is shared with other services in the same place.

something like

http://1.2.3.4/index.html

and my service sits as follows

const server = http.createServer((req, res) => {
const pathname = url.parse(req.url).pathname;
const jsHeader = { 'Content-Type': 'application/javascript' };
const cssHeader = { 'Content-Type': 'text/css' };
const textHeader = { 'Content-Type': 'text/plain' };

const jsBundleLink = `<${ FRAGMENT_EXTERNAL_LINK }/client/bundle.js>; 
rel="fragment-script"`;
const cssBundleLink = `<${ FRAGMENT_EXTERNAL_LINK }/client/bundle.css>; 
rel="fragment-script"`;

switch (pathname) {
  case '/client/bundle.js':
     res.writeHead(200, jsHeader);
     return fs.createReadStream(
      `${ WORKING_DIRECTORY }/client/bundle.js`).pipe(res);
  case '/client/bundle.css':
      res.writeHead(200, cssHeader);
      return fs.createReadStream(
        `${ WORKING_DIRECTORY }/client/bundle.css`).pipe(res);
  case '/healthcheck':
      res.writeHead(200, textHeader);
      return res.end('');
  default:
      res.writeHead(200, {
         'Content-Type': 'text/html',
         'Link': `${ jsBundleLink },${ cssBundleLink }`,
       });
      return res.end('');
      }
  });`

When I setup the load balancer the request comes in as

http://balancer.aws.com/microspas/mymicrospa/index.html

and this changes the pathname from

/client/bundle.js to microspas/mymicrospa/client/bundle.js

this is problematic for me as my application now has to be aware of the subpathing setup by aws elb.

Any way to not have the path changed or easily have my app know that it exists in a subpath or not so if I hit the direct url OR the load balanced url both still work?

some things to note:

  1. I need to stick with HTTP.CreateServer and I need the subpathing in the loadbalancer since there are other items in the load balanced cluster.

An example showing how this would work in node would be a big help, and if I can get this fixed at the alb level that would be even better.

EDIT: I accidentally wrote ELB in the original post but its an ALB. However issue still remains. I want to not have the basepath sent in from the ALB.

ie. path:www.contoso.com/albpath/client.js should be sent to my app as /client.js not albpath/client.js

You can migrate to an Application load balancer and use target groups for path based routing.

Path Conditions

You can use path conditions to define rules that forward requests to different target groups based on the URL in the request (also known as path-based routing).

Each path condition has one path pattern. If the URL in a request matches the path pattern in a listener rule exactly, the request is routed using that rule.

A path pattern is case-sensitive, can be up to 128 characters in length, and can contain any of the following characters. Note that you can include up to three wildcard characters.

 A–Z, a–z, 0–9 _ -. $ / ~ " ' @: + & (using &amp;) * (matches 0 or more characters)? (matches exactly 1 character)

Example path patterns

/img/* /js/*

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.

在此处输入图像描述

https://docs.aws.amazon.com/elasticloadbalancing/latest/application/tutorial-load-balancer-routing.html

https://docs.aws.amazon.com/elasticloadbalancing/latest/application/load-balancer-listeners.html

This is not a direct solution to this problem but I was facing the same problem and decided to use Host Based Routing instead of Path based Routing to solve this problem.

So if your host name was example.com, instead of configuring the application load balancer to route requests for example.com/abc, you can configure it to route your requests for abc.example.com.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM