I have multiple services that run under the /service/<service_name> block, each one for each service. This works just fine but it's really long and bloated since every block has blocks on it's on. An example:
location /service/firstservice {
proxy_pass http://localhost:8501/;
.
.
.
location /service/firstservice/static {
proxy_pass http://127.0.0.1:8501/static/;
}
location /service/firstservice/healthz {
proxy_pass http://127.0.0.1:8501/healthz;
}
location /service/firstservice/vendor {
proxy_pass http://127.0.0.1:8501/vendor;
}
location /service/firstservice/stream {
proxy_pass http://127.0.0.1:8501/stream;
.
.
.
}
}
And that's only for one service. I was thinking about making only one block with the base subdirectory and a map that points to the respective port:
map $uri $serviceport {
default invalid;
~^/service/firstservice/.* 8501;
~^/service/secondservice/.* 8502;
~^/service/thirdservice/.* 8503;
}
server {
...
location /service/(?<servicename>.+)$/ {
proxy_pass http://localhost:$serviceport;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
location /service/$servicename/static {
proxy_pass http://localhost:$serviceport/static/;
}
...
}
}
However nginx test always fails indicating that either the regex is bad or the logic is either not supported or just bad implementation.
Any guide or help will be highly appreciated!