Rewrite Proxy where the destination host ip is passed in the URI

I need to create a proxy rewrite in NGINX where the rewrite URL is composed of data included in the URI of the request. For instance:

Given the URL:

http://proxyhost:5555/go/222.222.222.222/action/etc

The rewrite should head to:

http://222.222.222.222:6666/action/etc

Is this possible in NGIX ?

I can do this on apache httpd but so far I have not figured out how to do this on nginx

(apache specific example):

RewriteEngine On
RewriteBase "/"
RewriteRule "^go/(.*)/action/(.*)$" "http://$1:6666/action/$2"  [P]
0
задан 15 April 2017 в 19:43
1 ответ

Этот блок location должен выполнять работу.

location ~ ^/go/([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})/(action/.+)$ {
    proxy_pass http://$1:6666/$2;
}

Я предполагаю, что здесь целевой хост всегда указывается как IP-адрес. Регулярное выражение примерно соответствует IP-адресу. При желании его можно сделать более или менее конкретным.

В любом случае, это делается в nginx с помощью захвата регулярных выражений в директиве location , а затем с использованием захваченных переменных в proxy_pass ] заявление.

1
ответ дан 4 December 2019 в 16:17

Теги

Похожие вопросы