When using variables, rewriting the URI on Nginx does not work

I have this atlas:

map $id $backend_host {
default http://primary -host;
1 http://primary-host;
2 http://secondary-host;
}

The following locations:

location / {
set $id 1;
proxy_pass $backend_host;
}

location /second/ {
set $id 2;
proxy_pass $backend_host/;
rewrite ^/second(.*)$$1 break;
}

When publishing to http: // nginx -host / second / some / uri, I found that it belongs to the second location clause and the URI is being rewritten, but then it is proxied to the primary host instead of the secondary host.

,When I set the second location:

location /second/ {
proxy_pass http://secondary-host/;
rewrite ^/ second(.*)$$1 break;
)

That is, without using variables, it can work normally.

Cannot be explained in the document Find any content.

try this

map $id $backend_host {
default primary-host;
1 primary-host;
2 secondary-host;
}

location / {
set $id 1;
pr oxy_pass http://$backend_host;
}

location /second/ {
set $id 2;
proxy_pass http://$backend_host/;
rewrite ^/second(.*)$$1 break;
}

http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass

< p>

or even like this:

proxy_pass $request;

In this case ,Search the server group described by the server name
, if not found, use a to determine the
parser.

Note: I did not test it

I have this atlas:

map $id $backend_host {
default http://primary-host;
1 http://primary-host;
2 http://secondary-host;
}

The following locations:

location / {
set $id 1;
proxy_pass $backend_host;
}

location /second/ {
set $id 2;
proxy_pass $backend_host/;
rewrite ^/second(.*)$$1 break;
}

When publishing to http: // nginx-host / second / some / uri, I found that it belongs to the second location clause, and the URI is being rewritten, but then it is being proxied to the primary host instead of the secondary host.

On the other hand, when I Set the second position Time:

location /second/ {
proxy_pass http://secondary-host/;
rewrite ^/second(.*)$$1 break ;
)

That is to say, without using variables, it will work normally.

Nothing can be found in the document explaining the document.

Try this

map $id $backend_host {
default primary-host;< br /> 1 primary-host;
2 secondary-host;
}

location / {
set $id 1;
proxy_pass http:/ /$backend_host;
}

location /second/ {
set $id 2;
proxy_pass http://$backend_host/;
rewrite ^ /second(.*)$$1 break;
}

http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass

or even like this:

proxy_pass $request;

In this case, search for the server name
The described server group, if not found, use a to determine the
parser.

Note: I did not test it

Leave a Comment

Your email address will not be published.