C
C
Cruach2019-08-18 16:26:06
Nginx
Cruach, 2019-08-18 16:26:06

How to change request in nginx with proxy_pass?

There is a task to redirect request. In a simple way, everything works fine:

location /api/v2/test/query {
    proxy_set_header Host $http_host;
    proxy_pass http://127.0.0.1:11111/api/v1/test/query;
}

But this does not work with variables, which is natural.
location /api/v2/test/-|[0-9a-fA-F]+/query {
    proxy_set_header Host $http_host;
    proxy_pass http://127.0.0.1:11111/api/v1/test/-|[0-9a-fA-F]+/query;
}

You need to do a rewrite and replace v2 with v1 . The -|[0-9a-fA-F]+ part must be passed on as received in the request. I am unable to do this. The question, as I understand it, is primitive, but it does not work out and I have to ask for help.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
dodo512, 2019-08-18
@dodo512

location ~ ^/api/v2/test/((-|[0-9a-fA-F]+)/query.*) {
    proxy_set_header Host $http_host;
    proxy_pass http://127.0.0.1:11111/api/v1/test/$1;
}

L
Lynn "Coffee Man", 2019-08-18
@Lynn

And what does not work in such a simple version?

location /api/v2/test/ {
    proxy_set_header Host $http_host;
    proxy_pass http://127.0.0.1:11111/api/v1/test/;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question