docker - 为docker-compose Web应用程序使用与localhost不同的主机名/URL

标签 docker nginx docker-compose

摘要:2个单独的应用程序,都使用docker-compose,如何才能同时提供http://app-1.testhttp://app-2.test
描述:
我觉得我错过了一些 super 简单的东西。我有2个php-fpm(通过nginx)应用程序,都由类似的docker-compose设置运行,有点像:

# docker-compose.yaml
version: '3'

services:
  app:
    build:
      context: .
      dockerfile: docker/Dockerfile
    container_name: app_1
    tty: true
    depends_on:
      - db
      - dbtest
    working_dir: /var/www
    volumes:
      - ./:/var/www

  webserver:
    image: nginx:stable
    container_name: app_1_webserver
    restart: always
    ports:
      - "80:80"
    depends_on:
      - app
    volumes:
      - ./:/var/www
      - ./docker/app.conf:/etc/nginx/conf.d/default.conf
    links:
      - app
# ...
在我的/etc/hosts上,我可以添加类似
127.0.0.1 app-1.test
现在,我可以转到浏览器app-1.test来调用该应用程序。
第二个也有类似的设置,但是由于端口80被阻塞,它当然不会上升。我当然可以更改端口,但是URL类似于app-2.test:81而不是app-2.test。我该怎么办,以便可以使用其他本地主机名运行第二个应用程序?还是使用其他端口是最好的方式?

最佳答案

你不能您可以做的是在图像(第三个图像)的前面添加一个“路由器”,该路由器根据主机名进行路由(代理传递)。
Apache或Nginx通常用于这类事情。
例如与Apache服务器
https://httpd.apache.org/docs/2.4/howto/reverse_proxy.html

<VirtualHost *:80>
    ServerName app-1.test
    ProxyRequests Off
    ProxyPreserveHost On
    ProxyPass / http://image1:80/
    ProxyPassReverse / http://image1:80/
    ErrorLog /var/log/apache2/error.log
    LogLevel info
    CustomLog /var/log/apache2/access.log combined
</VirtualHost>
<VirtualHost *:80>
    ServerName app-2.test
    ProxyRequests Off
    ProxyPreserveHost On
    ProxyPass / http://image2:80/
    ProxyPassReverse / http://image2:80/
    ErrorLog /var/log/apache2/error.log
    LogLevel info
    CustomLog /var/log/apache2/access.log combined
</VirtualHost>
现在,您可以在/ etc / hosts文件中的同一IP上添加两个名称,并且服务器可以根据提供的主机名( ServerName )在内部进行路由。http://image1:80/(及其类似)引用应更改为docker内部dns,如docker-compose.yml中指定的

关于docker - 为docker-compose Web应用程序使用与localhost不同的主机名/URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63833507/

相关文章:

linux - Nginx: curl: (60) SSL证书问题:无法获取本地颁发者证书

docker - Docker撰写共享文件夹

IIS 等效于 "proxy_set_header X-Forwarded-Proto https;"

docker - 如果您在 Docker 中使用 Spring Boot 应用程序,那么使用 SSL 的最佳方法是什么

bash - 在 docker-compose 命令中运行 stack build --file-watch 时如何修复 "<stdin>: hGetLine: end of file"

linux - centos 7 : nginx Failed to read PID from file/run/nginx. pid:参数无效

ruby-on-rails - 发送重定向到特定端口

docker - Docker容器如何相互访问

ssl - 如何在 docker 中为 SSL 添加正确的标签?

c# - 无法在 docker 容器内访问带有 dotnet/angular spa 应用程序的 API,但可以在 docker 容器外访问