gpt4 book ai didi

loops - Nginx - 在 proxy_pass 中添加变量时无限重新加载

转载 作者:行者123 更新时间:2023-12-01 12:50:34 25 4
gpt4 key购买 nike

我在 Docker 上使用 Nginx,我想为每个用户分配不同的端口。

首先,在不添加任何内容的情况下,我的代码运行良好:

    location  /viewer/ {
proxy_pass http://xx.xxx.xxx.xxx:18080/Road/;
}

正如预期的那样,转到 URL 中的“/viewer/”将代理到端口 18080。

但是如果我向 proxy_pass 添加任何变量,例如:

set $test 1;
proxy_pass http://xx.xxx.xxx.xxx:18080/Road/?$test;

然后,首先,不再加载静态文件,我必须添加如下行:

    location ~ \.css {
add_header Content-Type text/css;
}
location ~ \.js {
add_header Content-Type application/x-javascript;
}

在此之后,静态文件再次工作,但页面开始无限重新加载。

之前我认为这是因为我用 proxy_pass 中的变量替换了端口,但正如我向您展示的那样,当我在那里添加任何变量时就会发生这种情况。

你认为我会做错什么?感谢您的帮助!

最佳答案

将变量添加到 proxy_pass 会改变它的行为。您将需要构建整个 URI。

在您的原始配置中,URI /viewer/foo 在向上游传递之前被转换为 /Road/foo

在您的新配置中,URI /viewer/foo 被转换为 /Road/?1 并且原始 URI 的尾部丢失了。

使用 rewrite...break 修改 URI 可能会更成功。

例如:

location  /viewer/ {
rewrite ^/viewer(.*)$ /road$1?something break;
proxy_pass http://xx.xxx.xxx.xxx:18080;
}

参见 this document了解详情。


根据您的意见,您希望更改目标端口。

例如:

location  /viewer/ {
rewrite ^/viewer(.*)$ /road$1 break;
proxy_pass http://xx.xxx.xxx.xxx:$myport;
}

如果您通过 IP 地址指定上游服务器,则不需要 resolver 语句。但是,如果您通过名称指定上游,则需要定义一个 resolver。参见 this document了解详情。

关于loops - Nginx - 在 proxy_pass 中添加变量时无限重新加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58163580/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com