How to forward all paths that start with a specific location in nginx?

喜你入骨 提交于 2019-12-25 01:48:57

问题


I want to forward all paths that start with /api/ (/api/* ??) to port 1000 but the actual configuration either forwards only the paths that contain "/api/" and nothing else after (/api/login is not forwarded)

location /api/ {
                    proxy_pass http://localhost:1000/;
            }

or it doesn't work at all

location ~ ^/api/(.*)$ {
                    proxy_pass http://localhost:1000/;
            }

. The server is cinfigured as fallows:

server {
            listen       80;
            keepalive_timeout    70;
            server_name  server_name;

            location / {
                    root /var/www/html;
                    index index.html;
            }
            location /api/ {
                    proxy_pass http://localhost:1000/;
            }
            }

I would appreciate any help, Thank you!


回答1:


Note that with:

location /api/ {
    proxy_pass http://localhost:1000/;
}

If there is request /api/foo, then your API server will see /foo.

If, on the other hand (note there is no trailing slash in proxy_pass) you use:

location /api/ {
    proxy_pass http://localhost:1000;
}

Then for the same request, your API server will receive request "as is": /api/foo.

So make sure you use the right approach (slash / no slash) which depends on how your API server handles URLs (if it is configured to handle /api/foo URLs then you should not use trailing slash in the proxy_pass.



来源:https://stackoverflow.com/questions/58911675/how-to-forward-all-paths-that-start-with-a-specific-location-in-nginx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!