使用nginx和docker从URL中删除端口

时间:2020-03-06 18:06:24

标签: docker nginx docker-compose reverse-proxy

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

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

问题

dev-dev.website.com:5000

uat-dev.website.com

我想要实现的目标

dev-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 conf

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;
    }
}

nginx conf for uat

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;
    }
}

1 个答案:

答案 0 :(得分:0)

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