1

I've got a particular scenario where I'm needing to route to a different backend based on query parameter:

https://edge1.cdn.com/file.zip?{secure_link}&{tokens}&route=aws1

Where aws1 would be say http://edge1.amazonwebservices.com

and if its aws2 then proxy backend would be http://edge2.amazonwebservices.com

and so on... but I still have not figured out how to do this.

1 Answer 1

2

You can use map directive to get a proxy hostname from the $arg_route variable (which contains a value of the route query argument):

map $arg_route $aws {
    aws1    edge1.amazonwebservices.com;
    aws2    edge2.amazonwebservices.com;
    ...
    default <default_hostname>;
}

server {
    ...
    # if you want to proxy the request, you'd need a 'resolver' directive
    resolver <some_working_DNS_server_address>;

    location / {
        # if you want to proxy the request
        proxy_pass http://$aws;
        # or if you want to redirect the request
        rewrite ^ http://$aws$uri permanent;
    }
}

If you don't want to serve the request without route query argument, you can omit the last default line at the map block and add the following if block to your server configuration:

if ($aws = '') {
    return 403; # HTTP 403 denied
}

If you need to proxy the request you'd additionally need a resolver directive (you can read some technical details about it in this article).

Sign up to request clarification or add additional context in comments.

3 Comments

Anything helpful in the nginx error log?
I got excited with the first part of the answer that I forgot to read the last part of the resolver configuration, found it :) and now working like charm. thanks
Yes, I'd better add an example to the nginx configuration. Done. BTW, StackOverflow way of thanking for the answer described here :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.