Good day to all!

Please tell me something is not visible obvious solutions. Is it possible to proxy the incoming request to NGINX depending on where the request came from ( $http_referer )?

In other words, let's say a request came to test-domain.com , with domain10.com then the request should be 172.20.20.10 at 172.20.20.10 . If you came from domain11.com then it will be proxied at 172.20.20.11 . Here is a dependency.

Is it possible to parse the number from the url of the referrer using NGINX tools and then substitute it into the ip address? Or do you have to call some external script that will handle all this?

Thank.

    1 answer 1

    Try this option:

     location / { if ($http_referer ~* "domain(\d{1,2}).com") { proxy_pass http://172.20.20.$1/; } } 

    Addition in a type of specification in the comment.

    Try setting a match using the map directive, like this:

     map $http_referer $proxy_address { domain10.com 172.20.20.10; domain11.com 172.20.20.11; domain12.com 172.20.20.57; } 

    If there are a lot of values, then write instead of these lines include <путь_к_файлу_со_значениями> and specify the mappings in a separate file. And already in the directive proxy_pass write like this - proxy_pass http://$proxy_address;

    • Great, and where you can read about the fact that regular expressions can match variables, just like you give in the example. I did not meet something though I was looking for it. - Mihail Politaev
    • Although, although ... It turns out everything is somewhat more complicated. We need a separate table that will store the mapping of numbers in the domain and ip addresses for which to proxy. Since these numbers do not match. How to be here? On each request, call a script that will return a matching value? - Mihail Politaev
    • Nginx uses PCRE (perl compatible regular extensions - Perl-compatible regular expressions), so you can read about it on the web. I like this article - citforum.ru/internet/perl/regexp . And if you just want examples in brief, here they are - nginx.org/ru/docs/http/ngx_http_rewrite_module.html#if - MANKK
    • OK thanks. And what about the table mapping numbers in domain names and real ip addresses, do not tell me? - Mihail Politaev
    • @MishaPolitaev, updated the answer. - MAN69