Score:0

Multiple path in Nginx

cn flag

I am trying nginx for the first time and I am running it locally. I have been able to get my services up but I have a puzzling question because I run a Microservice and during upgrade I want to be able to just block a particular service.

Now, each service has a a path eg

\api\v1\wallet \api\v1\card

the issue I have is that both wallet and card path are in the same service.

If I have different paths would I have to duplicate or there's a way I could make it work better?

Here is my conf file

worker_processes 4;

events { worker_connections 1024; }
http {

    server {

        listen 80;
        charset utf-8;

        location ~ ^/api/v1/user {
            rewrite ^/api/v1/user/(.*) /$1 break;
            proxy_pass http://user-service:3001;
            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 /api/v1/wallet/ {
            # rewrite /api/v1/wallet/(.*) /$1 break;
            proxy_pass http://wallet-service:3007/api/v1/wallet/;
            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 /api/v1/card/ {
            # rewrite /api/v1/wallet/(.*) /$1 break;
            proxy_pass http://wallet-service:3007/api/v1/card/;
            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;
        }

    }


}
in flag
What is the question? If an upstream server is not available nginx returns a proxy error no matter in what location it happens...
King avatar
cn flag
I’m asking if some sort of wild card can be accepted so I am not duplicating location with just different paths.
Score:0
us flag

You can use the following setup:

location /api/v1/user {
    ...
}

location ~ ^/api/v1/(wallet|card)/$ {
    proxy_pass http://wallet-service:3007/api/v1/$1/;
    ...
}

Here we use regular expression capture to get path component into $1 variable, and then use it in proxy_pass destination.

If you want to pass the rest of the URL via proxy_pass, then you need a second capture:

location ~ ^/api/v1/(wallet|card)/(.*)$ {
    proxy_pass http://wallet-service:3007/api/v1/$1/$2;
    ...
}
King avatar
cn flag
the second suggestion returns `nginx-proxy_1 | 2022/03/06 13:11:48 [error] 32#32: *4 no resolver defined to resolve wallet-service, client: 172.20.0.1, server: , request: "GET /api/v1/card/health HTTP/1.1", host: "localhost" nginx-proxy_1 | 172.20.0.1 - - [06/Mar/2022:13:11:48 +0000] "GET /api/v1/card/health HTTP/1.1" 502 157 "-" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/15.1 Safari/605.1.15"`
King avatar
cn flag
The first one too returns 502 error for me.
us flag
You need to define name resolvers with `resolver` directive in nginx if you want to use names for your backend services. Alternative is to use IP addresses.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.