M
M
March772020-10-09 01:27:30
Nginx
March77, 2020-10-09 01:27:30

How to implement NGINX proxy with adding header when URL matches?

How to implement NGINX as a proxy with basic authorization headers added by URL match?

There is a server on which it is planned to deploy NGINX, let it be localhost. I want to use it as a proxy, which, if the URL matches, will add a basic authorization header.

Example, using a proxy to open google.com and add header
Authorization: Basic YWxhZGRpbjpvcGVuc2VzYW1l to the request

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dodo512, 2020-10-09
@dodo512

proxy_set_header

As a value, you can use text, variables, and their combinations.
If the value of the header field is an empty string, then the field will not be transmitted to the proxied server at all.

Using map , we check if $uri matches the desired value and assign the new variable the value "Basic YWxhZGRpbjpvcGVuc2VzYW1l" or an empty string.
map $uri $a {
    default      "";
    ~^/some_uri  "Basic YWxhZGRpbjpvcGVuc2VzYW1l";
}

proxy_set_header Authorization $a;

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question