I have a server that returns a 301 HTTP redirect url which contains an API key. The redirect hits Nginx and in there I need to add an Authorization
HTTP Header that contains the value of the API key. I want to then remove the API key from the query parameters that get sent through
I need to translate /google/?search=abcde&apikey=1234&version=1
to /google/?search=abcde&version=1
Code
location /google/ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_set_header Authorization "Bearer $arg_apikey";
proxy_pass https://google.com/;
}
I have tried the following, but it doesn't work: Remove parameters within nginx rewrite
location /google/ {
if ($query_string ~ "^(.*)apikey=(.*)$") {
rewrite ^(.*)$ $uri? permanent;
}
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_set_header Authorization "Bearer $arg_apikey";
proxy_pass https://google.com/;
}
Any help would be greatly appreciated!