【docker】利用docker来配置Nginx反向代理

目录

1、启动docker服务

2、下载Nginx镜像

3、Nginx镜像测试运行

 4、容器部署

5、配置

​编辑

 6、反向代理配置


1、启动docker服务

[root@mgr1 ~]# systemctl start docker.service 

2、下载Nginx镜像

[root@mgr1 ~]# docker pull nginx

3、Nginx镜像测试运行

[root@mgr1 ~]# docker run -d --name nginx -p 8080:80 nginx
156650e444db5b42d60b9d75e94ed864b845ef67021aa10df8ec8c44a7c1d386

-d是后台运行,--name是容器名字,-p是<本地映射端口>:<容器端口>,Nginx是镜像名

查看运行的容器

[root@mgr1 ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                                   NAMES
156650e444db   nginx     "/docker-entrypoint.…"   30 seconds ago   Up 29 seconds   0.0.0.0:8080->80/tcp, :::8080->80/tcp   nginx

检查一下

【docker】利用docker来配置Nginx反向代理_第1张图片

 4、容器部署

拷贝容器内的nginx配置文件到宿主机,再个容器目录的映射

1)先在宿主机创建三个文件

#创建www目录
[root@mgr1 ~]# mkdir nginx/www -p
#创建日志目录
[root@mgr1 ~]# mkdir nginx/logs -p
#创建配置目录
[root@mgr1 ~]# mkdir nginx/conf -p

2)拷贝配置文件 

[root@mgr1 ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                                   NAMES
156650e444db   nginx     "/docker-entrypoint.…"   20 minutes ago   Up 20 minutes   0.0.0.0:8080->80/tcp, :::8080->80/tcp   nginx
[root@mgr1 ~]# docker cp 156650e444db:/etc/nginx/nginx.conf ~/nginx/conf
[root@mgr1 ~]# ll nginx/conf/
total 4
-rw-r--r--. 1 root root 648 Jul 19 23:06 nginx.conf

3) 映射容器目录

#先删掉容器,因为端口被占用了
[root@mgr1 ~]# docker ps 
CONTAINER ID   IMAGE     COMMAND                  CREATED          STATUS          PORTS                                   NAMES
156650e444db   nginx     "/docker-entrypoint.…"   29 minutes ago   Up 29 minutes   0.0.0.0:8080->80/tcp, :::8080->80/tcp   nginx
[root@mgr1 ~]# docker stop nginx 
nginx
[root@mgr1 ~]# docker rm nginx 
nginx
#容器目录映射
[root@mgr1 ~]#docker run -d -p 8080:80 --name nginx -v ~/nginx/conf/nginx.conf:/etc/nginx/nginx.conf -v ~/nginx/logs:/var/log/nginx  -v ~/nginx/www:/usr/share/nginx/html   nginx
[root@mgr1 ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED         STATUS         PORTS                                   NAMES
c53a0cfc0423   nginx     "/docker-entrypoint.…"   7 seconds ago   Up 7 seconds   0.0.0.0:8080->80/tcp, :::8080->80/tcp   nginx

5、配置

[root@mgr1 ~]# cd nginx/www/
[root@mgr1 www]# mkdir www.haha.com
[root@mgr1 www]# cd www.haha.com/
[root@mgr1 www.haha.com]# echo hello > index.html
[root@mgr1 www.haha.com]# vim /etc/hosts
127.0.0.1 www.haha.com
#配置主配置文件,在http里添加以下字段
[root@mgr1 ~]# vim nginx/conf/nginx.conf 
 server {
        listen 80;
        server_name www.haha.com
        root  /usr/share/nginx/html/www.haha.com;    #容器的目录

        location / {
            root  /usr/share/nginx/html/www.haha.com;
            index index.html index.htm index.php;
        }
    }
#重启容器
[root@mgr1 ~]# docker ps
CONTAINER ID   IMAGE     COMMAND                  CREATED       STATUS       PORTS                                   NAMES
c53a0cfc0423   nginx     "/docker-entrypoint.…"   3 hours ago   Up 3 hours   0.0.0.0:8080->80/tcp, :::8080->80/tcp   nginx
[root@mgr1 ~]# docker restart c53a0cfc0423
c53a0cfc0423

验证 

 6、反向代理配置

#在之前的配置上,在location加上proxy_pass https://www.baidu.com;
[root@mgr1 ~]# vim nginx/conf/nginx.conf
server {
        listen 80;
        server_name www.haha.com
        root  /usr/share/nginx/html/www.haha.com;

        location / {
            root  /usr/share/nginx/html/www.haha.com;
            index index.html index.htm index.php;
            proxy_pass https://www.baidu.com;
        }
    }
#重启容器
[root@mgr1 ~]# docker restart c53a0cfc0423
c53a0cfc0423

验证

【docker】利用docker来配置Nginx反向代理_第2张图片

你可能感兴趣的:(docker,Nginx,docker,nginx,linux,centos,容器)