Api nginx中的代理-转发位置

Api nginx中的代理-转发位置,api,nginx,proxy,nginx-reverse-proxy,nginx-location,Api,Nginx,Proxy,Nginx Reverse Proxy,Nginx Location,假设我有一个REST API在server example.com上的localhost上侦听,并且在proxy.conf文件中有以下代理: location / { proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_pass http://127.0.0.1:8080/; } 这意味着我可以访问我的API,例如example.com/users。但是,此设置引起了

假设我有一个REST API在server example.com上的localhost上侦听,并且在proxy.conf文件中有以下代理:

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

这意味着我可以访问我的API,例如example.com/users。但是,此设置引起了一些冲突,因此我需要它的位置如下:

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

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 /
}
我可以使用example.com/API正确访问API的根目录。但是,它返回的链接位于context example.com/users等中,而不是我需要的example.com/api/users


本质上,我是在问,如何使用example.com/API/而不是example.com将请求转发给我的API?

解决了!如果有人遇到这种情况,我的解决方案如下:

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

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 /
}

感谢伊万提供的帮助

阅读。谢谢@IvanShatsky解决了我的问题!我以前从未需要过代理,所以我甚至不知道该搜索什么,更不用说如何解决它了