本文目标:在公网和内网情况下通过Dockerfile构建处nginx容器,并在不进入容器的情况下,修改nginx配置文件,最后通过shell脚本的形式将配置修改成功

准备配置文件

nginx:conf

# 全局配置
user root;
worker_processes auto;
error_log /var/log/nginx/error.log warn;

events {
    worker_connections  1024;
}

# http 配置
http {
    include /etc/nginx/mime.types;
    default_type application/octet-stream;
    
    # 访问日志配置
    access_log /var/log/nginx/access.log;
	
   charset utf-8;
   charset_types text/css text/plain application/json application/javascript;
	 
   map $http_upgrade $connection_upgrade {
       default       keep-alive;   #默认为keep-alive 可以支持一般http请求
       'websocket'   upgrade;      #如果为websocket 则n为upgrade可升级的。
   }

    # 服务器块
    server {
        charset utf-8;
        listen 80;
        
        server_name localhost;
		
		client_max_body_size 1024m;
        proxy_read_timeout 3600;
        proxy_send_timeout 3600;
        proxy_connect_timeout 3600;

		 
		location ^~ /collect_java/ {
          proxy_redirect     off;
          proxy_set_header   Host             $host;
          proxy_set_header   X-Real-IP        $remote_addr;
          proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
          proxy_set_header   Upgrade          $http_upgrade;#此处配置 上面定义的变量
          proxy_set_header   Connection       $connection_upgrade;
          proxy_set_header   Origin           *;
          proxy_pass http://172.17.0.1:8080/;
        }

        location ^~/upload/ {
          add_header Access-Control-Allow-Origin *;
          add_header Access-Control-Allow-Credentials true;
          add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS, PUT, DELETE';
          add_header Access-Control-Allow-Headers *;
          alias /home/upload/;
          autoindex on;
        }

        location / {
      		add_header Access-Control-Allow-Origin *;
      		add_header Access-Control-Allow-Credentials true;
      		add_header Access-Control-Allow-Methods 'GET, POST, OPTIONS, PUT, DELETE';
      		add_header Access-Control-Allow-Headers *;
            root /home/web;
            index index.html;
        }

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

配置文件有可能会出现如下问题:

问题1:nginx: [emerg] "server" directive is not allowed here in /usr/local/nginx/conf/nginx.conf:1
nginx: configuration file /usr/local/nginx/conf/nginx.conf test failed

解决方法:server 标签需要在 http的标签内  

正确的配置写法 : http {

server {}

}

问题2: nginx: [error] invalid PID number "" in "/usr/local/nginx/logs/nginx.pid"

解决方法:执行  /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf

问题3:nginx: [emerg] no "events" section in configuration

解决方法:在nginx.conf  添加  

events {
    worker_connections  1024;
}

 公网情况下

Dockerfile

# 使用基础镜像
FROM ubuntu:22.04

# 添加镜像的维护者信息
LABEL maintainer="mjz"

# 安装 Nginx 和所需软件
RUN apt-get update && apt-get install -y wget nginx locales

# 设置中文环境
RUN locale-gen zh_CN.UTF-8
ENV LANG zh_CN.UTF-8
ENV LANGUAGE zh_CN:zh
ENV LC_ALL zh_CN.UTF-8


# 暴露容器的 80 端口
EXPOSE 80

# 指定容器启动时执行的命令,让 Nginx 前台运行
CMD ["nginx", "-g", "daemon off;"]

build.sh

#编译Dockerfile文件
docker build -t mynginx .
#启动容器
docker run -d --restart always -p 80:80 --name mynginx -v /home/web:/home/web mynginx

delete.sh

如果构建错误可以执行这个脚本

#停止容器
docker stop mynginx
#删除容器
docker rm mynginx
#删除镜像
docker rmi mynginx

restart.sh

如果需要修改nginx.conf,就修改上面的配置文件,并执行这个脚本

#复制最新的nginx.conf到容器
docker cp nginx.conf mynginx:/etc/nginx/nginx.conf
#重启容器
docker restart mynginx

内网情况下

内网需要先向本地导入一个镜像,然后再使用Dockerfile进行构建

可以在公网将镜像进行打包

docker save -o mynginx.tar mynginx

然后在内网下进行载入

docker load -i mynginx.tar

然后通过docker images进行查看镜像是否载入成功

Dockerfile

# 使用基础镜像
FROM mynginx

# 添加镜像的维护者信息
LABEL maintainer="cntf@qq.com"

# 安装 Nginx 和所需软件
RUN apt-get update && apt-get install -y wget nginx locales

# 设置中文环境
RUN locale-gen zh_CN.UTF-8
ENV LANG zh_CN.UTF-8
ENV LANGUAGE zh_CN:zh
ENV LC_ALL zh_CN.UTF-8


# 暴露容器的 80 端口
EXPOSE 80

# 指定容器启动时执行的命令,让 Nginx 前台运行
CMD ["nginx", "-g", "daemon off;"]

build.sh

#编译Dockerfile文件
docker build -t nginx .
#启动容器
docker run -d --restart always -p 80:80 --name nginx -v /home/web:/home/web nginx

 delete.sh

如果构建错误可以执行这个脚本

#停止容器
docker stop nginx
#删除容器
docker rm nginx
#删除镜像
docker rmi nginx

restart.sh

如果需要修改nginx.conf,就修改上面的配置文件,并执行这个脚本

#复制最新的nginx.conf到容器
docker cp nginx.conf nginx:/etc/nginx/nginx.conf
#重启容器
docker restart nginx

补充

以上所有的东西需要在同一文件夹下!!!!

以上所有的东西需要在同一文件夹下!!!!

以上所有的东西需要在同一文件夹下!!!!

build.sh、delete.sh、restart.sh这些文件在创建之后,可能没有权限,可以使用如下命令赋权

chmod +x 文件名

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐