nginx - 具有相同域的 80 端口上的多个 Docker 容器

标签 nginx docker digital-ocean

我的问题类似于 this问题,但只有一个域。

是否可以在同一台服务器上运行多个 docker 容器,它们都在端口 80 上,但 URL 路径不同?

例如:

在内部,所有应用程序都托管在同一个 docker 服务器上。

172.17.0.1:8080 => app1
172.17.0.2:8080 => app2
172.17.0.3:8080 => app3

在外部,用户将通过以下 URL 访问应用程序:

www.mydomain.com                 (app1)
www.mydomain.com/app/app2        (app2)
www.mydomain.com/app/app3        (app3)

最佳答案

我使用 nginx 反向代理解决了这个问题。

这是 nginx 容器的 Dockerfile:

FROM nginx
COPY nginx.conf /etc/nginx/nginx.conf

这是nginx.conf:

http {

        server {
              listen 80;

              location / {
                proxy_pass http://app1:5001/;
              }

              location /api/ {
                proxy_pass http://app2:5000/api/;
              }
        }
}

然后我在同一个 docker 网络中建立了 nginx、app1 和 app2 容器。

确保在位置和代理路径中包含尾随 /,否则 nginx 将返回 '502: Bad Gateway'。

所有请求都通过 docker 主机的 80 端口,将它们交给 nginx 容器,然后由 nginx 容器根据 url 路径将它们转发到应用程序容器。

关于nginx - 具有相同域的 80 端口上的多个 Docker 容器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36393734/

相关文章:

java - 我可以将DigitalOcean上的JSP页面与LEMP一起使用吗?

SSL Forge、Cloudflare 和 Digital Ocean

linux - https ://example. com打开一个BIN文件供下载而不是页面内容

php - 多个 Nginx + PHP 容器

nginx - 使用 nginx 作为 websocket 连接的代理

docker - 如何在 dockerfile 中取消设置 "ENV"?

Nginx - 如果 $request_uri 存在 proxy_pass 一个 url,否则另一个 url

php - CentOS 7.x 上 Laravel : PHP 7. x 的 Docker 容器

node.js - 带有 nodeBB redis 的 Docker 配置不正确?

symfony - 如何将 Dockerized Symfony 应用程序部署到 Digital Ocean