Edd Grant
Edd Grant

Reputation: 774

AWS API Gateway not reverse proxying correctly

I am trying to use AWS API Gateway as a reverse HTTP proxy to front several prototype web applications which are deployed as elastic beanstalk apps:

Configuration

Each EB app is mapped in API Gateway under it's app name e.g.

/hello1 (API Gateway mapping) --> prototype1 EB application 
/hello2 (API Gateway mapping) --> prototype2 EB application
... etc

This is the overall mapping I have created:

enter image description here

It seems that API Gateway treats requests for the root resource in each mapping e.g. '/' differently to requests for sub resources, so I have set up proxy mappings for both:

The root ('/') resource is a straight forward (i.e. non-greedy) proxy mapping:

Root '/' resource proxy mapping

Then there's a greedy proxy mapping for anything underneath the root context:

Greedy proxy mapping under the root context

I then deploy the API to a stage named 'master'

The idea being that to access each prototype I'd GET the URL http://protos.acme.com/<stage>/<proto_name> e.g. http://protos.acme.com/master/hello1.

Problem

I'm finding that if I add a trailing slash to the URL, or specify a particular resource e.g. index.html then everything works perfectly e.g.

However if I omit the trailing slash from the URL e.g. http://protos.acme.com/master/hello1 then the URL rewriting done by API Gateway stops working and any URLs in the proxied content no longer resolve correctly as they omit the prototype name e.g. http://protos.acme.com/master/index.html.

I can't find any way to configure the behaviour for when the trailing slash is omitted and I don't think it's acceptable to force my users to always remember the trailing slash.

Grateful for any insight!

Edd

Upvotes: 3

Views: 3334

Answers (2)

jancy
jancy

Reputation: 1

I was facing the same problem, am using AWS API gateway with custom domain and aws-serverless-proxy to handle the api in the node.js backend.

If my custom domain URL - https://host.com, basepath - test, then with proxy integration the valid accessible url - https://host.com/test/ (With slash)

So, I am redirecting the url in the backend, if the incoming req url doesn't end with '/'.

Sample code: Reads the incoming request and redirects the response to the same url but add slash at the end.

let requestUrl = req.url;
    if (requestUrl !== undefined && !(requestUrl.endsWith('/'))) {
        res.writeHead(302, { 'Location': `${req.url}/` });
        res.end();
    }

Upvotes: 0

Dave Maple
Dave Maple

Reputation: 8422

I setup a quick test and it seems to work if I explicitly define a GET method at the resource root path in addition to the ANY {proxy+} path.

api gateway proxy integration

Upvotes: 3

Related Questions