很难弄清楚这一点。
我在 home.domain.com 上运行 nginx。
在我的家庭网络中,我有多个 Web 服务,我想通过反向代理从外部访问这些服务。目前没有 SSL,但我稍后会添加。我需要在 home.domain.com/app{1-3} 的子目录上设置反向代理,因为我只有 home.domain.com 的有效证书。
我当前的配置:
server {
listen 80 default;
keepalive_timeout 120;
server_name home.domain.com;
location /app1/ {
proxy_pass http://internalIP1:8081/;
}
location /app2/ {
proxy_pass http://internalIP2:5000/;
}
}
当我尝试访问 home.domain.com/app1 时,它应该重定向到 home.domain.com/app1/login/?next=%2F,而是转到 home.domain.com/login/?next=% 2F。这显然会抛出 404,因为它在 nginx 服务器上不可用。
想法?有建议吗?
如果您的应用程序不是webapi项目,它有html页面。您可以在页面中设置a。然后修改你的 nginx 配置,如下所示:
location ^~ /app1/ {
proxy_pass http://internalIP1:8081/;
proxy_set_header xxxxxx;
.... ;
sub_filter '<base href="/" />' '<base href="/app1/" />';
}
它将把您的 html 页面的
<base href="/" />
更改为 <base href="/app1/" />
。
the sub_filter directive is --with-http_sub_module in nginx.
sudo nginx -t // check this config.
sudo nginx -s reload //reload the nginx config
它对我有用。 如果您的页面未设置,您可以添加
sub_filter '<head>' '<head><base href="aap1" />'