docker-compose快速启动nginx

前言
本文包括如下部分:
1)docker-copose快速启动nginx
2)开发环境快速启动nginx的一个方案。

1. 容器启动nginx

1.1 docker-compose 文件

创建nginx目录,目录下创建docker-compose.yml文件如下:

version: "3"
services:
  nginx-02:
   #我这里是内网镜像,替换成你可用的镜像
    image: "harbocto.xxx.com.cn/public/nginx"
    restart: on-failure
    ports:
      - 80:80
    volumes:
      - ./nginx.conf:/etc/nginx/conf.d/default.conf:ro
      - ./build:/usr/share/nginx/html
    restart: always

1.2 nginx.conf

nginx目录下创建创建nginx.conf文件,根据实际情况配置,我这里写一个示例:

# gzip设置
gzip on;
gzip_vary on;

gzip_comp_level 6;
gzip_buffers 16 8k;

gzip_min_length 1000;
gzip_proxied any;
gzip_disable "msie6";
#gzip_http_version 1.0;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript;

server {
    listen       80;
    server_name  web80;

    location /images/ {
        root /root/build;
        autoindex on;
    }
    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
        add_header Cache-Control no-store;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

}

1.3 静态文件

在nginx目录下创建build目录,将前端静态文件拷贝到下边

1.4 启动

在nginx目录下执行如下命令启动服务

 # docker-compose up -d

启动之后,nginx就可以正常使用了。

2. 自动创建脚本

说明:
1)在宿主机中执行如下脚本,自动启动一个容器供开发测试使用。
2)执行过程中需要一些交互式输入:安装位置、使用端口。
3)开发环境可从FTP或Http服务器上调用该脚本直接本地启动一个nginx实例。

#!/bin/bash
########## 定义变量 ##########
read -p "输入安装的位置(回车默认/usr/local/nginx ) " nginx_dir
if [ -z "${nginx_dir}" ];then
        nginx_dir=/usr/local/nginx
fi


read -p "输入外部端口(默认80):" nginx_port
if [ -z "${nginx_port}" ];then
        nginx_port=80
fi

############## yml文件 ##################
mkdir ${nginx_dir}/build -p
cat > ${nginx_dir}/docker-compose.yml << EOF
version: "3"
services:
  nginx-02:
    # 我这里是内网镜像,替换成你可用的镜像
    image: "10.252.xxx.x'x'x/public/nginx"
    restart: on-failure
    ports:
      - ${nginx_port}:80
    volumes:
      - ./nginx.conf:/etc/nginx/conf.d/default.conf:ro
      - ./build:/usr/share/nginx/html
    restart: always
EOF

########## config ################
cat > ${nginx_dir}/nginx.conf << EOF
# gzip设置
gzip on;
gzip_vary on;

gzip_comp_level 6;
gzip_buffers 16 8k;

gzip_min_length 1000;
gzip_proxied any;
gzip_disable "msie6";
#gzip_http_version 1.0;
gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript;

server {
    listen       80;
    server_name  web80;



    location /images/ {
        root /root/build;
        autoindex on;
    }

    location /admin/ {
        root /root/build;
        autoindex on;
    }



    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
        add_header Cache-Control no-store;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

}

EOF

############ index.html ##################
chmod 755 ${nginx_dir}/build

cat > ${nginx_dir}/build/index.html << EOF
Please upload the file to ${nginx_dir}/build/
EOF

chmod 644 ${nginx_dir}/build/index.html



############## start ####################

cd ${nginx_dir}
docker-compose up -d
docker ps

echo "nginx is running "
echo "Please upload the file to ${nginx_dir}/build/ "


  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

玄德公笔记

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

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

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

打赏作者

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

抵扣说明:

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

余额充值