I have 2 location blocks with proxy_pass configs, they look in a simplified form like this:
location ^~ /location1/ {
set $backend1 https://service1.example.com;
proxy_pass $backend1;
}
location ^~ /location2/ {
rewrite /location2/(.*) /new/base/path/$1 break;
set $backend2 https://service2.example.com;
proxy_pass $backend2;
}
Now the problem is location1
works fine, but location2
doesn't, it returns 500, and I get this error in the logs
2022/09/26 18:07:42 [error] 25952#25952: *263 invalid URL prefix in "", client: 123.123.123.123, server: www.example.com, request: "GET /location2/my/url HTTP/1.1", host: "www.example.com", referrer: "https://www.example.com/referrer/page"
If I set the value with a variable, like this:
location ^~ /location2/ {
rewrite /location2/(.*) /new/base/path/$1 break;
proxy_pass https://service2.example.com;
}
then it works. I need to use a variable though, because it helps forcing nginx re-resolve the DNS (as mentioned in this solution)
What I noticed from the error log line is that the variable $backend2
is empty, which gets clearer if I define it like this
set $backend2 service2.example.com;
proxy_pass https://$backend2;
which will out put an error like this instead:
2022/09/26 18:07:42 [error] 25952#25952: *263 invalid URL prefix in "https://", client: 123.123.123.123, server: www.example.com, request: "GET /location2/my/url HTTP/1.1", host: "www.example.com", referrer: "https://www.example.com/referrer/page"
So what could I be missing?