nginx proxy_pass省略路径 [英] nginx proxy_pass omitting path

查看:345
本文介绍了nginx proxy_pass省略路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经配置了Nginx反向代理:

I have configured a nginx reverse proxy:

location / {
        root /var/www/html;
        index index.html;
}


location /login {
        proxy_pass http://127.0.0.1:9080;

        proxy_set_header        Host                    $host;
        proxy_set_header        X-Real-IP               $remote_addr;
}


location /app {
        rewrite ^/app/(.*)$ /$1 break;
        proxy_pass https://10.11.12.13/1020/;

        proxy_set_header        Host                    $host;
        proxy_set_header        X-Real-IP               $remote_addr;
}

在端口9080上侦听的服务器将重定向到路由/app/{生成的子路径}. IP 10.11.12.13上的服务器在{generated subpath}上处理请求

The server listening on port 9080 redirects to the route /app/{generated subpath}. The server on IP 10.11.12.13 processes the request on {generated subpath}

Nginx不在10.11.12.13上游服务器上使用完整路径,而忽略了/1020/端点.发生这种行为的原因可能是什么?

Nginx doen't use the full path on the 10.11.12.13 upstream server, omitting the /1020/ endpoint. What could be the reason for this behaviour ?

推荐答案

文档指出:

使用rewrite在代理位置内更改URI时 指令,并且将使用相同的配置来处理 request(break)...在这种情况下,指令中指定的URI 将被忽略,并将更改后的完整请求URI传递到服务器.

When the URI is changed inside a proxied location using the rewrite directive, and this same configuration will be used to process a request (break) ... In this case, the URI specified in the directive is ignored and the full changed request URI is passed to the server.

因此,您可以使用rewrite...break,例如:

So, you can either use a rewrite...break, for example:

location /app {
    rewrite ^/app/(.*)$ /1020/$1 break;
    proxy_pass https://10.11.12.13;
    ...
}

或者,您可以让locationproxy_pass语句执行相同的转换,例如:

Or, you can let the location and proxy_pass statements perform the same transformation, for example:

location /app {
    proxy_pass https://10.11.12.13/1020;
    ...
}

请注意,在后一种情况下,为了进行正确的转换,两个值都应以/结尾或都不以/结尾.

Note that in this latter case, for correct transformation, both values should end with a / or neither end with a /.

这篇关于nginx proxy_pass省略路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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