Docker部署nginx泛型二级域名分配

1. 先安装docker 环境

  1. 卸载之前的docker,贴脚本,进行回车
	sudo yum remove docker \
                  docker-client \
                  docker-client-latest \
                  docker-common \
                  docker-latest \
                  docker-latest-logrotate \
                  docker-logrotate \
                  docker-engine

  1. 安装必要的依赖 ,贴脚本,进行回车
sudo yum install -y yum-utils \
device-mapper-persistent-data \
lvm2

  1. 设置docker仓库,如要设置阿里云镜像仓库可以先自行百度 [访问这个地址,使用自己的阿里云账号登录,查看菜单栏左下角,发现有一个镜像加速器:https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors]
    在这里插入图片描述
  2. 复制阿里云的加速镜像加载配置
sudo yum-config-manager \
  --add-repo \
  https://download.docker.com/linux/centos/docker-ce.repo

  1. 安装docker
sudo yum install -y docker-ce docker-ce-cli containerd.io
  1. 启动docker 且 设置开机启动
sudo systemctl start docker;sudo systemctl enable docker

2. nginx 部署

  1. 下载nginx 镜像
docker pull nginx

在这里插入图片描述

  1. 创建nginx 的主机挂载地址,根据需求创建多个web项目
mkdir -p /opt/nginx/org/html /opt/nginx/org/logs /opt/nginx/org/conf
mkdir -p /opt/nginx/web/html /opt/nginx/web/logs /opt/nginx/web/conf
mkdir -p /opt/nginx/file/html /opt/nginx/file/logs /opt/nginx/file/conf
mkdir -p /opt/nginx/proxy/html /opt/nginx/proxy/logs /opt/nginx/proxy/conf
mkdir -p /opt/nginx/person/html /opt/nginx/person/logs /opt/nginx/person/conf

在这里插入图片描述

  1. 运行一个默认的nginx配置,复制脚本,回车
docker run --name nginx-test -p 8080:80 -d nginx

在这里插入图片描述
4. 查看是否运行

docker ps

在这里插入图片描述
5. 将配置,同步到上面创建的文件夹中在这里插入图片描述

docker cp 82e072900107:/etc/nginx/nginx.conf /opt/nginx/org/conf
docker cp 82e072900107:/usr/share/nginx/html/index.html /opt/nginx/org/html
docker cp 82e072900107:/usr/share/nginx/html/50x.html /opt/nginx/org/html

docker cp 82e072900107:/etc/nginx/nginx.conf /opt/nginx/web/conf
docker cp 82e072900107:/usr/share/nginx/html/index.html /opt/nginx/web/html
docker cp 82e072900107:/usr/share/nginx/html/50x.html /opt/nginx/web/html

docker cp 82e072900107:/etc/nginx/nginx.conf /opt/nginx/file/conf
docker cp 82e072900107:/usr/share/nginx/html/index.html /opt/nginx/file/html
docker cp 82e072900107:/usr/share/nginx/html/50x.html /opt/nginx/file/html

docker cp 82e072900107:/etc/nginx/nginx.conf /opt/nginx/proxy/conf
docker cp 82e072900107:/usr/share/nginx/html/index.html /opt/nginx/proxy/html
docker cp 82e072900107:/usr/share/nginx/html/50x.html /opt/nginx/proxy/html

docker cp 82e072900107:/etc/nginx/nginx.conf /opt/nginx/person/conf
docker cp 82e072900107:/usr/share/nginx/html/index.html /opt/nginx/person/html
docker cp 82e072900107:/usr/share/nginx/html/50x.html /opt/nginx/person/html
  1. 复制上面脚本,一次性执行,相当于将默认配置,每个项目都加上了配置项
    在这里插入图片描述
  2. 复制脚本 , 执行项目nginx 命令,启动项目

 docker run -p 80:80 --name nginx-proxy -v /opt/nginx/proxy/logs/:/var/log/nginx -v /opt/nginx/proxy/conf/nginx.conf:/etc/nginx/nginx.conf -v /opt/nginx/proxy/html:/usr/share/nginx/html -d nginx

 docker run -p 10086:80 --name nginx-person -v /opt/nginx/person/logs/:/var/log/nginx -v /opt/nginx/person/conf/nginx.conf:/etc/nginx/nginx.conf -v /opt/nginx/person/html:/usr/share/nginx/html -d nginx

 docker run -p 10087:80 --name nginx-file -v /opt/nginx/file/logs/:/var/log/nginx -v /opt/nginx/file/conf/nginx.conf:/etc/nginx/nginx.conf -v /opt/nginx/file/html:/usr/share/nginx/html -d nginx

 docker run -p 10088:80 --name nginx-web -v /opt/nginx/web/logs/:/var/log/nginx -v /opt/nginx/web/conf/nginx.conf:/etc/nginx/nginx.conf -v /opt/nginx/web/html:/usr/share/nginx/html -d nginx

 docker run -p 10089:80 --name nginx-org -v /opt/nginx/org/logs/:/var/log/nginx -v /opt/nginx/org/conf/nginx.conf:/etc/nginx/nginx.conf -v /opt/nginx/org/html:/usr/share/nginx/html -d nginx



执行成功
在这里插入图片描述

docker ps

在这里插入图片描述
访问每一个端口的地址是否能访问:IP:端口,80 端口,可以不用加
在这里插入图片描述

配置nginx反向代理

  1. 前提条件,咱们已经有域名解析到当前的ip,且必须要做一个泛解析域名,我已经有了,这个域名解析每个域名商都有解析说明,可以参考,我这边就不说了
  2. 以80 端口的地址为主,咱们这边使用的proxy
  3. 进入到proxy 的nginx 配置地址: cd /opt/nginx/proxy/conf/
    在这里插入图片描述
  4. 修改nginx.conf 配置项,已经有一个默认配置,只需要简单修改即可
    在这里插入图片描述
  5. 整体修改后的配置nginx.conf文件,这个配置文件的原理就是
    1. 用户访问 test.com 是进入80端口监听,80端口直接转发到index.html
    2. 用户访问 person.test.com 当前配置也解析了person.test.com的配置,进入到80端口监听,但是当前的person.test.com 是转发到的 test.com:10086 的端口的,并不是转发到当前index的,所以直接反向代理到了我们内网正常使用的localhost:端口号 的项目地址

user  nginx;
worker_processes  1;

error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {

    gzip on;

    gzip_min_length 1K;

    gzip_buffers 4 16k;

    gzip_http_version 1.1;

    gzip_comp_level 1;

    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript image/jpeg image/gif image/png;

    gzip_proxied off;
    
    gzip_vary on;

    gzip_disable msie6;

    server {
        listen       80;         #监听的端口
        server_name  test.com;    #监听的URL
        root  /usr/share/nginx/html;           #项目路径
        index index.html;
        # Any route that doesn't have a file extension (e.g. /devices)
        location / {
            try_files $uri $uri/ /index.html;
        } 
    }
    server {
        listen       80;         #监听的端口
        server_name  school.test.com;    #监听的URL
        location / {
           proxy_redirect off;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://test.com:10089;

            gzip on;
            gzip_http_version 1.1;
            gzip_comp_level 1;
            gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
            root  html;
            index  index.html index.htm;
        }
    }
    server {
        listen       80;         #监听的端口
        server_name  web.test.com;    #监听的URL
        location / {
           proxy_redirect off;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://test.com:10088;

            gzip on;
            gzip_http_version 1.1;
            gzip_comp_level 1;
            gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
            root  html;
            index  index.html index.htm;
        }
    }
    server {
        listen       80;         #监听的端口
        server_name  file.test.com;    #监听的URL
        location / {
           proxy_redirect off;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://test.com:10087;

            gzip on;
            gzip_http_version 1.1;
            gzip_comp_level 1;
            gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
            root  html;
            index  index.html index.htm;
        }
    }
	server {
        listen       80;         #监听的端口
        server_name  person.yqnedu.com;    #监听的URL
        location / {
           proxy_redirect off;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://test.com:10086;

            gzip on;
            gzip_http_version 1.1;
            gzip_comp_level 1;
            gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
            root  html;
            index  index.html index.htm;
        }
    }
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    keepalive_timeout  65;

    #gzip  on;

    include /etc/nginx/conf.d/*.conf;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

java 凯

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值