mardi 29 juin 2021

Replace first part of nginx $uri before proxy_pass

I have following location ngix config:

  location /bar {
    proxy_pass http://bar-api/api; 
  }

I can test it with curl

$ curl foo.com/bar/test
{"test passed":true}

But IP address of internal server bar-api could change, so I can not specify it directly as above.

I understand, I need to use variables in the location block and specify resolver. Resolver is already specified in http block of nginx config resolver 192.168.0.11 valid=10s;

I'm not sure how to modify location block, so it will work the same way as before. I tried this:

  location /bar {
    set $target http://bar-api/api;
    proxy_pass $target; 
  }

But test fails:

$ curl foo.com/bar/test  
<getting 404>

Also tried:

  location /bar {
    set $target bar-api;
    proxy_pass http://$target/api; 
  }

Test still fails:

$ curl foo.com/bar/test  
<getting 404>

Probably $uri should be modified with regex. This part /bar/test should be /api/test So I could use it in location.

  location /bar {
    set $target bar-api;
    <some magic here>
    proxy_pass http://$target/$modified_uri; 
  }

But how to do it?




Aucun commentaire:

Enregistrer un commentaire