0

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!

1 Answer 1

1

At first, I don't understand why are you using those nested locations at your first example, it should be enough just to use

location /service/firstservice/ {
    proxy_pass http://localhost:8501/;
    ...
}

Next, you can't use a variable to specify a location (just answered the related question here). If you want to change an URI passed to the backend, you should use a rewrite directive to do it (just answered the related question here):

map $servicename $serviceport {
    firstservice        8501;
    secondservice       8502;
    thirdservice        8503; 
}
server {
    ...
    location ~ ^/service/(?<servicename>firstservice|secondservice|thirdservice)/ {
        rewrite ^/service/[^/]+(/.*) $1 break;
        proxy_pass http://127.0.0.1:$serviceport;
        ...
    }
    ...
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.