如何转发以nginx中的特定位置开头的所有路径? [英] How to forward all paths that start with a specific location in nginx?

查看:2837
本文介绍了如何转发以nginx中的特定位置开头的所有路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将所有以/api/(/api/* ??)开头的路径转发到端口1000,但实际配置要么只转发包含"/api/"的路径,然后再转发(/api/登录未转发)

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

或者根本不起作用

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

我将不胜感激,谢谢!

推荐答案

请注意:

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

如果有请求/api/foo,则您的API服务器将看到/foo.

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

如果另一方面(请注意,在proxy_pass中没有斜杠),请使用:

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

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

对于相同的请求,您的API服务器将按原样接收请求:/api/foo.

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

因此,请确保使用正确的方法(斜杠/不斜杠),这取决于您的API服务器处理URL的方式(如果将其配置为处理/api/foo URL,则不应在proxy_pass中使用尾部斜杠).

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.

这篇关于如何转发以nginx中的特定位置开头的所有路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆