0

Lets say I have a REST API listening on localhost on the server example.com, and I have the following proxy working in a proxy.conf file:

location / {
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr; 
    proxy_pass http://127.0.0.1:8080/;
}

This means I can access my API, e.g. example.com/users. However this setup has caused some conflict, so I instead need it with the location as follows:

location /api/ {
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr; 
    proxy_pass http://127.0.0.1:8080/;
}

I can correctly access the root of the API using example.com/api. However, the links it returns are in the context example.com/users etc, not example.com/api/users as I would need.

Essentially I am asking, how do I forward the request to my API with example.com/api/ instead of just example.com?

sks7
  • 1
  • 2
  • 1
    Read [this](https://stackoverflow.com/questions/53649885/a-little-confused-about-trailing-slash-behavior-in-nginx). – Ivan Shatsky Apr 07 '21 at 13:05
  • Thank you @IvanShatsky that solved my problem! I've never needed to proxies before, so I wasn't even sure what to search, let alone how to solve it – sks7 Apr 07 '21 at 13:54

1 Answers1

0

Solved! If anyone comes across this, my solution was as follows:

location /api/ {
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr; 
    proxy_pass http://127.0.0.1:8080; # note the removed trailing /
}

Thanks to Ivan for providing this link to help!

sks7
  • 1
  • 2