在我的Docker主机上。使用nginx容器,我尝试通过使用url名称来标识正确的服务(portainer,rancher)来反向代理多个服务。
来自https://host1/rancher的流量=> https / rancher-container
来自https://host1/portainer的流量=> http://portainer-container:9000
我将nginx配置为使用url重写来转换url,然后再将其发送到良好的服务。它适用于服务搬运工。但这不适用于牧场主2服务。
这是我的配置:
location /rancher/ {
rewrite ^/rancher^/ /$1 break;
proxy_pass https://rancher-container;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_http_version 1.1;
}
location /portainer/ {
rewrite ^/portainer^/ /$1 break;
proxy_pass http://portainer-container:9000/;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Host $server_name;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
当我从chromeDevTools中查看时,会看到:
GEThttps://host1/rancher/200确定(并返回索引页面是这样的):
<!DOCTYPE html>
<html>
...
<link id="vendor" rel="stylesheet" href="/assets/vendor.css">
...
<script src="/assets/vendor-ebb1f9e6b4381d69a55448a2a5d7e4c9.js"></script>
<script src="/assets/ui-72ee502ee50a84d0f416c3164137307d.js"></script>
...
GEThttps://host1/assets/vendor.cssnet :: ERR_ABORTED 404GEThttps://host1/assets/vendor-ebb1f9e6b4381d69a55448a2a5d7e4c9.jsnet :: ERR_ABORTED 404获取https://host1/assets/ui-72ee502ee50a84d0f416c3164137307d.js
[我认为我的网络浏览器试图获取在html页面上定义的资源(css,js,img..etc等),其相对URL像是'/ assets /'到'https://host1/assets'。或正确的网址是“ https://host1/racher/assets”。
是否有解决方案?
检查网站。大概在HTML代码中,您的网站不知道它不在额外的路径上运行,而是假定它在根目录上运行。也许有一些切换,您可以设置网站的基本路径来知道它的位置。
不幸的是,这是一个非常普遍的问题。默认代理标头不发送原始路径,因此隐藏在其后的服务通常不知道请求是针对/以外的其他内容。
如果代理后面的服务是您的,则可以传递一些其他标头,例如x-proxy-urlpath,并通知服务它应呈现包括该路径的链接。
如果没有,您将无法在网站中设置基本URL,最好的解决方案是使用子域和基于主机的代理。