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/ "
posted on 2021-09-15 17:48 运维开发玄德公 阅读(124) 评论(0) 编辑 收藏 举报 来源
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了