0

I want to pass different path to the same proxy_pass but I keep getting 502 Bad gateway.

These path use the same port number but different base path. How do I make it work from what I have which returns an error currently.

this is what my current location looks like

worker_processes 4;
# worker_process auto
events { worker_connections 1024; }

http {

    server {

        listen 80;
        charset utf-8;

        location ~ ^/api/v1/(wallet|card)/(.*)$ {
            proxy_pass http://wallet-service:3007/api/v1/$1/;
            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;
        }
    }
}
Ivan Shatsky
  • 13,267
  • 2
  • 21
  • 37
King
  • 1,885
  • 3
  • 27
  • 84
  • Does this answer your question? [Redirecting captured regex group using nginx](https://stackoverflow.com/questions/72260921/redirecting-captured-regex-group-using-nginx) – Ivan Shatsky May 28 '22 at 06:11
  • I don’t think so because the answer there means any path can be passed prior. I only want my own defined path like wallet or card – King May 28 '22 at 07:01
  • 1
    I think you didn't read the answer carefully (or did I wrote it so badly?) Use `location ~ /api/v1/(?wallet|card)/ { rewrite ^ /api/v1/$service/ break; proxy_pass http://wallet-service:3007; ... }` – Ivan Shatsky May 28 '22 at 08:17
  • So this works but something weird. every url now has to end with `/` and some endpoints that require no authorization are now returning 401 error. @IvanShatsky – King May 28 '22 at 14:03
  • I used rewrite rule similar to what you are using in your original question. If you need to pass the URI including the suffix after `(wallet|card)`, you don't need to rewrite anything at all, use `location ~ /api/v1/(wallet|card)/ { proxy_pass http://wallet-service:3007; ... }` – Ivan Shatsky May 28 '22 at 14:08
  • Can you add it as an answer so I select it. It works now. – King May 28 '22 at 14:14

1 Answers1

0

If you don't need an URI to be changed at all, don't use anything other than the upstream name:

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

If an URI needs to be rewritten before being passed to the upstream, check this answer to see how to do it.

Ivan Shatsky
  • 13,267
  • 2
  • 21
  • 37