【发布时间】:2020-05-29 22:19:54
【问题描述】:
我有一个简单的静态站点,由 Hugo 生成,我正在构建一个运行 Nginx 的 Docker 容器。 Nginx 正在侦听端口 90。我遇到了一些奇怪的行为,其中某些链接尝试打开 container 端口而不是主机端口(在localhost 的情况下,它是 8000)。例如,这个链接:
<a href="/documents">Docs</a>
...当鼠标悬停时显示它将尝试打开localhost:8000/documents,这是正确的,但是当单击它时,它会尝试打开http://localhost:90/documents/(如果我手动将浏览器中的URL更改为http://localhost:8000/documents/ ,它的反应很好。)
是什么让这更奇怪:
- 只有特定的链接,特别是在标题菜单中,才会这样做。
- 我使用了几十个 Hugo 主题,我只遇到过其中一个问题:ZDoc。它可以特定于这个主题吗?我觉得这很奇怪。
这可能是什么原因造成的?我什至不知道这种现象叫什么。 “主机/容器端口混淆”?
我确定这不是 Nginx 或 Docker 的错误配置。我在我的 Dockerfile 中正确地公开了端口 90:
EXPOSE 90
nginx.conf 设置为侦听该端口:
http {
include /etc/nginx/mime.types;
sendfile on;
server {
root /usr/share/nginx/html/;
index index.html;
server_name localhost;
listen 90;
}
}
我正在启动 Docker 容器,主机端口 8000 转发到 Nginx 正在侦听的端口:
docker run --name my-simple-site -p 8000:90 -d simple-site
$ docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
de9cd1526034 simple-site "nginx -g 'daemon of…" 41 minutes ago Up 41 minutes 0.0.0.0:8000->90/tcp my-simple-site
【问题讨论】: