通过子目录而非端口访问Docker应用的连接问题
IIS子目录反向代理Docker应用解决方案
一、修复IIS反向代理的静态资源加载问题
你之前的规则仅处理了主路径转发,但Docker应用输出的静态资源路径是根路径(如/css/style.css),浏览器会直接请求www.mywebsite.com/css/style.css而非/website3/css/style.css,因此需要配置入站转发规则+出站内容重写规则:
1. 配置入站反向代理规则
- 打开IIS管理器,定位到
www.mywebsite.com对应的主站点 - 进入「URL重写」模块,点击「添加规则」→ 空白规则
- 规则名称:
Forward to Website3 - 匹配URL:
- 请求路径:选择「正则表达式」,输入
^website3/(.*) - 忽略大小写:勾选
- 请求路径:选择「正则表达式」,输入
- 操作:
- 操作类型:「重写」
- 重写URL:
http://localhost:40444/{R:1} - 追加查询字符串:勾选
- 停止处理后续规则:取消勾选
- 规则名称:
2. 配置出站规则修正资源路径
- 同样在「URL重写」模块,点击「添加规则」→ 空白规则(出站)
- 规则名称:
Rewrite Website3 Static Resources - 匹配筛选器:
- 筛选器类型:「响应内容」
- 模式:
(href|src|action)="/(?!website3/)([^"]+)"(匹配根路径开头的资源引用)
- 条件:
- 条件输入:
{RESPONSE_CONTENT_TYPE} - 匹配类型:「包含」
- 模式:
text/html(仅处理HTML内容)
- 条件输入:
- 操作:
- 操作类型:「重写」
- 重写值:
$1="/website3/$2" - 重写内容:「HTML」
- 规则名称:
配置完成后,访问www.mywebsite.com/website3时,页面内的所有根路径资源都会被自动修正为/website3/xxx,解决404问题。
二、备选方案:使用Nginx(Windows版)
如果觉得IIS配置繁琐,Nginx的子目录代理配置更简洁:
- 下载Windows版Nginx并解压到服务器,将原IIS站点改到其他端口(如8080)
- 编辑
conf/nginx.conf,在http块内添加以下server配置:
server { listen 80; server_name www.mywebsite.com; # 转发IIS托管的website1 location /website1/ { proxy_pass http://localhost:8080/website1/; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; } # 转发IIS托管的website2 location /website2/ { proxy_pass http://localhost:8080/website2/; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; } # 转发Docker容器的website3 location /website3/ { proxy_pass http://localhost:40444/; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $remote_addr; # 自动修正页面内的资源路径 sub_filter 'href="/' 'href="/website3/'; sub_filter 'src="/' 'src="/website3/'; sub_filter 'action="/' 'action="/website3/'; sub_filter_once off; } }
- 启动Nginx,可通过第三方工具(如Nginx Service Manager)将其设置为Windows服务,实现开机自启。
内容的提问来源于stack exchange,提问作者lunar soap5
相关产品推荐
相关产品推荐

