# 九.Nginx

# 1.安装过程

# 1.1 获取 Nginx 镜像

docker pull nginx
1

# 2.拷贝配置

docker run --name nginx -p 90:80 -d nginx
1
  • 创建挂载目录
mkdir -p /xxx/nginx/conf
mkdir -p /xxx/nginx/log
mkdir -p /xxx/nginx/html
1
2
3
  • 修改配置文件为本地
docker cp nginx:/etc/nginx/nginx.conf /xxx/nginx/conf/nginx.conf
docker cp nginx:/etc/nginx/conf.d /xxx/nginx/conf/conf.d
docker cp nginx:/usr/share/nginx/html /xxx/nginx/
1
2
3
  • 删除容器
# 找到nginx对应的容器id
docker ps -a
# 关闭该容器
docker stop nginx
# 删除该容器
docker rm nginx
1
2
3
4
5
6

# 3.配置 nginx

    server {
        listen       80;
        server_name  81.71.127.69;
        location ~*^.+$ {
            root   /usr/src/zhoubichuan/prod;
            try_files $uri $uri/ /index.html;
            index  index.html index.htm;
        }
    }
1
2
3
4
5
6
7
8
9

# 4.启动镜像

docker run \
-p 80:80 \
--name nginx \
--network dockerbetweennetwork \
-v /xxx/nginx/conf/nginx.conf:/etc/nginx/nginx.conf \
-v /xxx/nginx/conf/conf.d:/etc/nginx.conf.d \
-v /xxx/nginx/log:/var/log/nginx \
-v /xxx/nginx/html:/usr/share/nginx/html \
-d nginx:latest
1
2
3
4
5
6
7
8
9
命令 描述
–name nginx 启动容器的名字
-d 后台运行
--network dockerbetweennetwork 桥接网络
-p 80:80 将容器的 80(后面那个) 端口映射到主机的 80(前面那个) 端口
-v /xxx/nginx/conf/nginx.conf:/etc/nginx/nginx.conf 挂载 nginx.conf 配置文件
-v /xxx/nginx/conf/conf.d:/etc/nginx.conf.d 挂载 nginx 配置文件
-v /xxx/nginx/log:/var/log/nginx 挂载 nginx 日志文件
-v /xxx/nginx/html:/usr/share/nginx/html 挂载 nginx 内容
nginx:latest 本地运行的版本
\ shell 命令换行