返回列表 发帖

CentOS8_在Docker中使用Nginx的反向代理

实验中,宿主机的操作系统的版本信息:
[root@centos8 ~]# cat /etc/redhat-release
CentOS Linux release 8.2.2004 (Core)
[root@centos8 ~]#
[root@centos8 ~]# uname -r
4.18.0-193.el8.x86_64


先安装依赖软件包:
[root@centos8 ~]# dnf -y install yum-utils

配置阿里云的Docker镜像地址:
[root@centos8 ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
添加仓库自:http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
[root@centos8 ~]#

生成缓存:
[root@centos8 ~]# dnf makecache
CentOS-8.5.2111 - Base - mirrors.aliyun.com                                                   38 kB/s | 3.9 kB     00:00   
CentOS-8.5.2111 - Extras - mirrors.aliyun.com                                                 15 kB/s | 1.5 kB     00:00   
CentOS-8.5.2111 - AppStream - mirrors.aliyun.com                                              32 kB/s | 4.3 kB     00:00   
Docker CE Stable - x86_64                                                                     62 kB/s |  31 kB     00:00   
元数据缓存已建立。

[root@centos8 ~]#


安装指定版本的Docker:
[root@centos8 ~]# dnf -y install docker-ce-20.10.21. docker-ce-cli-20.10.21 containerd.io-1.6.12


启动Docker:
[root@centos8 ~]# systemctl start docker
[root@centos8 ~]#

设置开机自动启动Docker:
[root@centos8 ~]# systemctl enable docker
Created symlink /etc/systemd/system/multi-user.target.wants/docker.service → /usr/lib/systemd/system/docker.service.
[root@centos8 ~]#
[root@centos8 ~]# systemctl is-enabled docker
enabled
[root@centos8 ~]#


启动Docker后,宿主机的网卡会发生变化:
[root@centos8 ~]# ifconfig
-bash: ifconfig: 未找到命令
[root@centos8 ~]#

[root@centos8 ~]# dnf -y install net-tools

[root@centos8 ~]# ifconfig
docker0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 172.17.0.1  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:47:f7:f6:b8  txqueuelen 0  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens160: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.168.154  netmask 255.255.255.0  broadcast 192.168.168.255
        inet6 fe80::92ab:5fb4:5373:ad53  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:cb:1e:40  txqueuelen 1000  (Ethernet)
        RX packets 211822  bytes 168761908 (160.9 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 151433  bytes 8990956 (8.5 MiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@centos8 ~]#


查看Docker的版本信息:
[root@centos8 ~]# docker -v
Docker version 20.10.21, build baeda1f
[root@centos8 ~]#


查看Docker的相关信息:
[root@centos8 ~]# docker info
Client:
Context:    default
Debug Mode: false
Plugins:
  app: Docker App (Docker Inc., v0.9.1-beta3)
  buildx: Docker Buildx (Docker Inc., v0.9.1-docker)
  scan: Docker Scan (Docker Inc., v0.21.0)

Server:
Containers: 0
  Running: 0
  Paused: 0
  Stopped: 0
Images: 0
Server Version: 20.10.21
Storage Driver: overlay2
  Backing Filesystem: xfs
  Supports d_type: true
  Native Overlay Diff: true
  userxattr: false
Logging Driver: json-file
Cgroup Driver: cgroupfs
Cgroup Version: 1
Plugins:
  Volume: local
  Network: bridge host ipvlan macvlan null overlay
  Log: awslogs fluentd gcplogs gelf journald json-file local logentries splunk syslog
Swarm: inactive
Runtimes: io.containerd.runc.v2 io.containerd.runtime.v1.linux runc
Default Runtime: runc
Init Binary: docker-init
containerd version: a05d175400b1145e5e6a735a6710579d181e7fb0
runc version: v1.1.4-0-g5fd4c4d
init version: de40ad0
Security Options:
  seccomp
   Profile: default
Kernel Version: 4.18.0-193.el8.x86_64
Operating System: CentOS Linux 8 (Core)
OSType: linux
Architecture: x86_64
CPUs: 1
Total Memory: 1.758GiB
Name: centos8.zhuohua.store
ID: E7QB:RF46:TK7K:ECJK:FSAU:ADU5:Z4LE:XQ4C:HTD2:KVQU:KDBJ:BHYF
Docker Root Dir: /var/lib/docker
Debug Mode: false
Registry: https://index.docker.io/v1/
Labels:
Experimental: false
Insecure Registries:
  127.0.0.0/8
Live Restore Enabled: false

[root@centos8 ~]#

注释:Docker的镜像、容器默认是存放在目录/var/lib/docker下



###

修改Docker的镜像、容器默认的存放目录:

[root@centos8 ~]# find / -name "docker.service"
/sys/fs/cgroup/cpu,cpuacct/system.slice/docker.service
/sys/fs/cgroup/blkio/system.slice/docker.service
/sys/fs/cgroup/memory/system.slice/docker.service
/sys/fs/cgroup/devices/system.slice/docker.service
/sys/fs/cgroup/pids/system.slice/docker.service
/sys/fs/cgroup/systemd/system.slice/docker.service
/usr/lib/systemd/system/docker.service
[root@centos8 ~]#


[root@centos8 ~]# vi /usr/lib/systemd/system/docker.service

ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
修改为:
ExecStart=/usr/bin/dockerd --graph="/data1/docker"

注释:新目录/data1/docker不存在的话,会自动创建的。


重启宿主机,使更改生效:
[root@centos8 ~]# reboot


宿主机重启后,查看Docker的相关信息:
[root@centos8 ~]# docker info

Docker Root Dir: /data1/docker

注释:现在Docker的镜像、容器默认是存放在目录/data1/docker下


[root@centos8 ~]# ls /data1/docker/
buildkit  containers  image  network  overlay2  plugins  runtimes  swarm  tmp  trust  volumes
[root@centos8 ~]#

[root@centos8 ~]# ls /var/lib/docker/
buildkit  containers  image  network  overlay2  plugins  runtimes  swarm  tmp  trust  volumes
[root@centos8 ~]#

可以删除旧目录/var/lib/docker:
[root@centos8 ~]# rm -rf /var/lib/docker
[root@centos8 ~]#



###

从公网下载镜像:( 以下是下载Nginx/1.12的镜像 )
[root@centos8 ~]# docker pull nginx:1.12
1.12: Pulling from library/nginx
f2aa67a397c4: Pull complete
e3eaf3d87fe0: Pull complete
38cb13c1e4c9: Pull complete
Digest: sha256:72daaf46f11cc753c4eab981cbf869919bd1fee3d2170a2adeac12400f494728
Status: Downloaded newer image for nginx:1.12
docker.io/library/nginx:1.12
[root@centos8 ~]#


查看宿主机的所有镜像:
[root@centos8 ~]# docker images
REPOSITORY   TAG       IMAGE ID       CREATED       SIZE
nginx        1.12      4037a5562b03   4 years ago   108MB
[root@centos8 ~]#


使用镜像“nginx:1.12”创建并启动容器“Nginx_01”:
[root@centos8 ~]# docker run -itd --name Nginx_01 -p 80:80 nginx:1.12
d33fdaa570fc42d19cffe596159474c5a01b274d7384542f9cb9ad0661c44c79
[root@centos8 ~]#

注释:
-itd : 以交互模式情况下后台运行。
--name : 指定容器名称。
-p 端口映射 : 第一个80是宿主机的端口,暴露给外部直接访问;第二个80是容器的端口。
nginx:1.12 : 镜像名称:版本号


在宿主机查看正在运行的容器:
[root@centos8 ~]# docker ps
CONTAINER ID   IMAGE        COMMAND                  CREATED              STATUS              PORTS                               NAMES
d33fdaa570fc   nginx:1.12   "nginx -g 'daemon of…"   About a minute ago   Up About a minute   0.0.0.0:80->80/tcp, :::80->80/tcp   Nginx_01
[root@centos8 ~]#

注释:宿主机的TCP 80端口,映射到容器“Nginx_01”的TCP 80端口。



在宿主机的防火墙firewalld打开TCP 80端口:
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --reload

查看防火墙所有打开的端口:
[root@centos8 ~]# firewall-cmd --zone=public --list-ports
80/tcp


在客户端访问容器“Nginx_01”的默认站点:
http://192.168.168.154:80/
图片1.png
2022-12-24 19:37


注释:此时,外面的客户端访问宿主机的TCP 80端口时,等于访问容器“Nginx_01”的TCP 80端口。



###

进入容器“Nginx_01”:
[root@centos8 ~]# docker exec -it Nginx_01 /bin/bash
root@d33fdaa570fc:/#

查看容器“Nginx_01”的系统版本信息:
root@d33fdaa570fc:/# cat /etc/issue
Debian GNU/Linux 9 \n \l

root@d33fdaa570fc:/# more /etc/debian_version
9.4
root@d33fdaa570fc:/# uname -r
4.18.0-193.el8.x86_64
root@d33fdaa570fc:/#


查看容器“Nginx_01”的Nginx版本:
root@d33fdaa570fc:/# nginx -v
nginx version: nginx/1.12.2
root@d33fdaa570fc:/#


修改容器“Nginx_01”的Nginx的默认首页文件的内容:
root@d33fdaa570fc:/# find / -name index.*
find: '/proc/7/map_files': Permission denied
/usr/share/nginx/html/index.html
root@d33fdaa570fc:/#

root@d33fdaa570fc:~# ls /usr/share/nginx/html  
50x.html  index.html
root@d33fdaa570fc:~#


root@d33fdaa570fc:/# vi /usr/share/nginx/html/index.html
bash: vi: command not found
root@d33fdaa570fc:/#

root@d33fdaa570fc:/# apt-get update
root@d33fdaa570fc:/# apt-get -y install vim


自定义新内容:
root@d33fdaa570fc:/# cat /usr/share/nginx/html/index.html
<!DOCTYPE html>
<html>
<head>
<title>Welcome to zhuohua</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to zhuohua!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. </p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>


再次,在客户端访问容器“Nginx_01”的默认站点:
http://192.168.168.154/
图片2.png
2022-12-24 19:40


注释:不指定端口时,默认就是使用TCP 80端口。



###

容器“Nginx_01”在宿主机的存放目录:
[root@centos8 ~]# cd /data1/docker
[root@centos8 docker]# ls
buildkit  containers  image  network  overlay2  plugins  runtimes  swarm  tmp  trust  volumes
[root@centos8 docker]#
[root@centos8 docker]# cd containers/
[root@centos8 containers]# ls
d33fdaa570fc42d19cffe596159474c5a01b274d7384542f9cb9ad0661c44c79
[root@centos8 containers]#

[root@centos8 containers]# pwd
/data1/docker/containers
[root@centos8 containers]#
[root@centos8 containers]# cd d33fdaa570fc42d19cffe596159474c5a01b274d7384542f9cb9ad0661c44c79/
[root@centos8 d33fdaa570fc42d19cffe596159474c5a01b274d7384542f9cb9ad0661c44c79]# ls
checkpoints                                                                hostconfig.json  mounts
config.v2.json                                                             hostname         resolv.conf
d33fdaa570fc42d19cffe596159474c5a01b274d7384542f9cb9ad0661c44c79-json.log  hosts            resolv.conf.hash
[root@centos8 d33fdaa570fc42d19cffe596159474c5a01b274d7384542f9cb9ad0661c44c79]#

容器“Nginx_01”的文件config.v2.json中的一些信息:

容器ID : "Config":{"Hostname":"d33fdaa570fc",

端口映射 :
"Ports":{"80/tcp":[{"HostIp":"0.0.0.0","HostPort":"80"},{"HostIp":"::","HostPort":"80"}]},





######

宿主机的Nginx版本:
[root@centos8 ~]# nginx -v
nginx version: nginx/1.14.1
[root@centos8 ~]#


宿主机的Nginx的主配置文件:
[root@centos8 ~]# cat /etc/nginx/nginx.conf

重新自定义了Nginx的默认站点的TCP端口:
    server {
        listen       81 default_server;
        listen       [::]:81 default_server;
        server_name  _;
        root         /usr/share/nginx/html;


保存文件退出后,重启Nginx服务:
[root@centos8 ~]# systemctl restart nginx
[root@centos8 ~]#


在宿主机的防火墙firewalld打开TCP 81端口:
firewall-cmd --zone=public --add-port=81/tcp --permanent
firewall-cmd --reload

再次,查看防火墙所有打开的端口:
[root@centos8 ~]# firewall-cmd --zone=public --list-ports
80/tcp 81/tcp




###

在宿主机中,把容器“Nginx_01”的目录/usr/share/nginx/html复制到宿主机的目录/home下:
[root@centos8 ~]# docker cp Nginx_01:/usr/share/nginx/html /home
[root@centos8 ~]#
[root@centos8 ~]# ls /home/
html
[root@centos8 ~]# ls /home/html/
50x.html  index.html
[root@centos8 ~]#


重新自定义宿主机的Nginx的默认首页文件的内容:
[root@centos8 ~]# mv -f /home/html/index.html /usr/share/nginx/html/index.html
[root@centos8 ~]#

[root@centos8 ~]# cat /usr/share/nginx/html/index.html
<!DOCTYPE html>
<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<head>
<title>Welcome to zhuohua</title>
<style>
    body {
        width: 35em;
        margin: 0 auto;
        font-family: Tahoma, Verdana, Arial, sans-serif;
    }
</style>
</head>
<body>
<h1>Welcome to zhuohua!</h1>
<p>这是宿主机的Nginx。</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
[root@centos8 ~]#

注释:
加上 <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 才能在浏览器里正常显示中文。


在客户端访问宿主机的默认站点:( 要使用TCP 81端口 )
http://192.168.168.154:81/
图片3.png
2022-12-24 19:44






######

在容器“Nginx_01”的Nginx中配置反向代理到宿主机的默认站点:

修改容器“Nginx_01”的Nginx的主配置文件:
root@d33fdaa570fc:~# find / -name 'nginx.conf'
/etc/nginx/nginx.conf
find: '/proc/7/map_files': Permission denied
root@d33fdaa570fc:~#

root@d33fdaa570fc:~# vi /etc/nginx/nginx.conf

插入:
server {
        location / {
          proxy_pass http://192.168.168.154:81;
                   }
       }

如下图:
图片4.png
2022-12-24 19:45



在宿主机重启容器“Nginx_01”:
[root@centos8 ~]# docker restart Nginx_01
Nginx_01
[root@centos8 ~]#


再次,在客户端访问容器“Nginx_01”的默认站点:
http://192.168.168.154/
图片5.png
2022-12-24 19:46


注释:此时,外面的客户端访问宿主机的TCP 80端口时,等于访问容器“Nginx_01”的TCP 80端口,也等于访问宿主机的TCP 81端口。





######

在宿主机的Nginx中创建两个基于域名的虚拟主机:

创建一个虚拟主机配置文件:( discuz.zhuohua.store )
[root@centos8 ~]# cat /etc/nginx/vhost/discuz.zhuohua.store.conf
server
    {
        listen 81; #此虚拟主机的TCP端口
        server_name discuz.zhuohua.store;
        index index.html index.htm index.php;
        root /wwwroot/discuz.zhuohua.store;
        include /etc/nginx/default.d/*.conf;
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
            expires      30d;
        }
        location ~ .*\.(js|css)?$
        {
            expires      12h;
        }
        location ~ /\.
        {
            deny all;
        }
        access_log off;
    }
[root@centos8 ~]#


创建虚拟主机的网站文件存放目录:
[root@centos8 ~]# mkdir -p /wwwroot/discuz.zhuohua.store

创建虚拟主机的默认首页文件:
[root@centos8 ~]# echo 'discuz.zhuohua.store' > /wwwroot/discuz.zhuohua.store/index.html



再创建一个虚拟主机配置文件:( bbs.zhuohua.store
[root@centos8 ~]# cat /etc/nginx/vhost/bbs.zhuohua.store.conf
server
    {
        listen 81; #此虚拟主机的TCP端口
        server_name bbs.zhuohua.store b.zhuohua.store;
        index index.html index.htm index.php;
        root /wwwroot/bbs.zhuohua.store;
        include /etc/nginx/default.d/*.conf;
        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
            expires      30d;
        }
        location ~ .*\.(js|css)?$
        {
            expires      12h;
        }
        location ~ /\.
        {
            deny all;
        }
        access_log off;
    }
[root@centos8 ~]#


创建虚拟主机的站点文件存放目录:
[root@centos8 ~]# mkdir -p /wwwroot/bbs.zhuohua.store

创建虚拟主机的默认首页文件:
[root@centos8 ~]#echo 'bbs.zhuohua.store' > /wwwroot/bbs.zhuohua.store/index.htm



编辑Nginx的主配置文件:
[root@centos8 ~]# vi /etc/nginx/nginx.conf

在文件最后那个大括号 } 上面插入以下代码:
include vhost/*.conf;  ##在目录/etc/nginx/vhost/下的.conf文件都会被加载

如下图:
图片6.png
2022-12-24 19:49



重启Nginx服务:
[root@centos8 ~]# systemctl restart nginx
[root@centos8 ~]#


Window客户端访问宿主机的两个基于域名的虚拟主机:(在没有DNS服务器解析域名的情况下,可以在文件hosts里绑定)
图片7.png
2022-12-24 19:49


http://discuz.zhuohua.store:81/
图片8.png
2022-12-24 19:50


http://bbs.zhuohua.store:81/
图片9.png
2022-12-24 19:50


http://b.zhuohua.store:81/
图片10.png
2022-12-24 19:50






######

在容器“Nginx_01”的Nginx中配置反向代理到宿主机基于域名的虚拟主机:

修改容器“Nginx_01”的Nginx的主配置文件:
root@d33fdaa570fc:~# vi /etc/nginx/nginx.conf

插入:
  upstream webservers_1{
    ip_hash;
    server 192.168.168.154:81;
   }

  server {
    listen 80;  #容器“Nginx_01”的Nginx的端口
    server_name discuz.zhuohua.store;
    location / {
    proxy_pass http://webservers_1;
    proxy_set_header Host $http_host;
   }
   }


  upstream webservers_2{
    ip_hash;
    server 192.168.168.154:81;
   }

  server {
    listen 80;  ##容器“Nginx_01”的Nginx的端口
    server_name bbs.zhuohua.store b.zhuohua.store;
    location / {
    proxy_pass http://webservers_2;
    proxy_set_header Host $http_host;
   }
   }

如下图:
图片11.png
2022-12-24 19:52


图片12.png
2022-12-24 19:52


注释:此时,外面的客户端访问Nginx的TCP 80端口时,等于访问192.168.168.154的TCP 81端口,并且区分了域名。



在宿主机重启容器“Nginx_01”:
[root@centos8 ~]# docker restart Nginx_01
Nginx_01
[root@centos8 ~]#


让容器“Nginx_01”随宿主机的启动而启动:(默认是不会随宿主机的启动而启动)
[root@centos8 ~]# docker update --restart=always Nginx_01
Nginx_01
[root@centos8 ~]#



Window客户端进行访问:

http://discuz.zhuohua.store/
图片13.png
2022-12-24 19:53


http://bbs.zhuohua.store/
图片14.png
2022-12-24 19:54


http://b.zhuohua.store/
图片15.png
2022-12-24 19:54


注释:此时,外面的客户端访问容器“Nginx_01”的基于域名的虚拟主机时,等于访问宿主机的基于域名的虚拟主机。





######

在宿主机中,把容器“Nginx_01”的文件/usr/share/nginx/html/50x.html复制到宿主机的目录/home下:
[root@centos8 ~]# docker cp Nginx_01:/usr/share/nginx/html/50x.html /home
[root@centos8 ~]#
[root@centos8 ~]# ls /home/
50x.html  html
[root@centos8 ~]#


在宿主机中,把宿主机的文件/usr/share/nginx/html/nginx-logo.png复制到容器“Nginx_01”的目录/root下:
[root@centos8 ~]# docker cp /usr/share/nginx/html/nginx-logo.png Nginx_01:/root
[root@centos8 ~]#


在宿主机中,把宿主机的目录/usr/share/nginx/html复制到容器“Nginx_01”的目录/var下:
[root@centos8 ~]# docker cp /usr/share/nginx/html Nginx_01:/var
[root@centos8 ~]#





相关文章:
CentOS8_使用Docker安装Python3

CentOS8_在Docker中安装LAMP
CentOS8_在Docker中安装LAMP(使用参数--link)

CentOS8_在Docker中安装LNMP(使用参数--link)
CentOS8_在Docker中安装LNMP

CentOS8_在Docker中安装vsftpd
CentOS8_在Docker中安装Samba
CentOS8_在Docker中安装Nagios
CentOS8_在Docker的容器内自动启动服务

CentOS8_在Docker中安装Oracle11gR2
CentOS8_在Docker中安装Oracle19c

CentOS6_Nginx反向代理+基于域名的虚拟主机
CentOS8_Nginx基于域名的虚拟主机+代理虚拟主机

返回列表