前言
本文包括如下部分:
1)docker-copose快速启动nginx
2)开发环境快速启动nginx的一个方案。
创建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
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; } }
在nginx目录下创建build目录,将前端静态文件拷贝到下边
在nginx目录下执行如下命令启动服务
# docker-compose up -d
启动之后,nginx就可以正常使用了。
说明:
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/ "