Score:0

Any way Nginx can resolve a CNAME (or look up a URL elsewhere) and then redirect to the result?

cn flag

I've got an AWS API Gateway endpoint with a URL like this:

https://xxxxxxxxxx-vpce-09572222209cd2305.execute-api.us-west-1.amazonaws.com

I want to create an easy to remember alias for that name that I can use in my browser. I've already got an Nginx gateway that is redirecting to a number of other servers. So I just had to add a few lines to that machine's config: Here's what I'm doing right now:

location /myapi {
    return 301 https://xxxxxxxxxx-vpce-09572222209cd2305.execute-api.us-west-1.amazonaws.com;
}

This works great. I can hit https://mydomain/myapi with my browser and I get redirected to the real AWS endpoint URL.

But then I do a redeploy, and the URL changes. With this setup, I have to log onto my Nginx server and update the above entry in my config file to redirect to the new endpoint.

I'm wondering if there is any way to have Nginx obtain that URL from some dynamic location that my deployment code can update so that I don't have to manually edit the config file whenever the endpoint URL changes. Using DNS is my first thought. Is there any way to cause Nginx to do a CNAME lookup and then redirect to the result? Can you think of another way that I can get this dynamic behavior from Nginx?

I can't do a proxy to the endpoint because of SSL. My browser has to end up hitting the AWS URL so that the address in the request matches up with Amazon's certificate. I think a redirect is the only way to do this.

I believe that there is a way to do this within API Gateway, but it's complicated. I want to find something that's easier to do...something I can do pretty much on the fly without a lot of thought. I'll want to use this same technique over and over.

us flag
I am not that knowledgeable about AWS, but I have understood that the AWS DNS can automatically update DNS entries for services when the endpoints change. You should look into AWS DNS to implement this. Using a separate nginx instance for this purpose sounds kludgy.
Score:0
vg flag

First off all - sorry for not really answering your question correctly. Is there any way of running a bash-script or something similar during the deploy? I was thinking you could run a check-hostname function on the aws-side that cen be used, put together a correct URL and change it in the nginx-config. Run nginx -s reload to hotswap the config and everything should be up to date. (might be good to run a nginx -t to check if the configuration is sane, but if I'm not misstaking nxinx -s reload stops if the configuration has clear errors.)

CryptoFool avatar
cn flag
Thanks for the thoughts. What I want is to not have to do anything. I want Nginx to not have to be restarted or redeployed. If the AWS endpoint gets redeployed, I want a way that Nginx can just just start serving the new URL without me having to do anything with it. I suppose I could write a cron job that dod a DNS lookup periodically, and reconfigured and restarted Nginx whenever DNS changed, but I was hoping there was a way to cause Nginx to just pick up the change by doing a lookup upon every request.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.