【问题标题】:Remove port from url with nginx and docker使用 nginx 和 docker 从 url 中删除端口
【发布时间】:2020-03-07 05:29:12
【问题描述】:

我有 2 个网站实例在同一台服务器上运行,用于 docker 容器上的开发和登台环境。这些站点的源代码相同,但位于 2 个单独的文件夹中。 /dev/uat 在同一台服务器上,可以通过单个域但不同的端口访问它们,即 dev.website.com:5000dev.website.com

我无权访问 DNS 也无法更改它,但我请求将域 uat.website.com 指向相同的 IP 地址,以便我可以删除该端口。这可以通过反向代理实现还是需要更改 DNS?

问题

开发 - dev.website.com:5000

uat - dev.website.com

我想要达到的目标

开发 - dev.website.com

uat - uat.website.com

docker-compose dev.yml

version: '3'

networks:
  dev_network:
    driver: bridge

services:
  web_dev:
    image: nginx:stable-alpine
    container_name: web_dev
    working_dir: /var/www
    volumes:
      - ./src:/var/www
      - ./nginx/default.dev.conf:/etc/nginx/conf.d/default.conf
    depends_on:
      - app_dev
    ports:
      - "5000:80"
    networks:
      - dev_network

  app_dev:
    image: php:7.2-fpm
    container_name: app_dev
    working_dir: /var/www
    volumes:
      - ./src:/var/www
    ports:
      - "9005:9000"
    networks:
      - dev_network


docker-compose uat.yml

version: '3'

networks:
  uat_network:
    driver: bridge

services:
  web_uat:
    image: nginx:stable-alpine
    container_name: web_uat
    working_dir: /var/www
    volumes:
      - ./src:/var/www
      - ./nginx/default.uat.conf:/etc/nginx/conf.d/default.conf
    depends_on:
      - app_uat
    ports:
      - "80:80"
    networks:
      - uat_network

  app_uat:
    image: php:7.2-fpm
    container_name: app_uat
    working_dir: /var/www
    volumes:
      - ./src:/var/www
    ports:
      - "9000:9000"
    networks:
      - uat_network

用于开发的 nginx 配置

server {
    listen 80;
    index index.php index.html;
    server_name localhost;
    root    /var/www;

    location / {
        try_files   $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass app_dev:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}

uat 的 nginx 配置

server {
    listen 80;
    index index.php index.html;
    server_name localhost;
    root    /var/www;

    location / {
        try_files   $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass app_uat:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}

【问题讨论】:

    标签: docker nginx docker-compose reverse-proxy


    【解决方案1】:

    根据我的经验,实现虚拟主机路由的最简单方法是将traefik 与 docker compose 结合使用。它将为您处理重定向,并且还可以在需要时终止 https(假设您可以通过不同的 dns 名称访问同一服务器。)。当然你也可以添加另一层 nginx 作为反向代理。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-20
      • 2015-10-21
      • 2020-03-02
      • 1970-01-01
      • 2018-01-31
      • 2014-07-18
      • 2011-07-04
      相关资源
      最近更新 更多