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/ "