目次Nginx镜像的获取
二者区别 主要区别在于定制化水平和构建过程的控制: 直接拉取Nginx镜像:
通过Dockerfile构建Nginx镜像:
总结,直接拉取Nginx镜像得当快速摆设和标准使用场景,而通过Dockerfile构建Nginx镜像得当必要特定配置、模块或主动化摆设流程的场景。选择哪种方式取决于项目标具体需求和开辟流程。 Nginx关键目次配置目次 (/etc/nginx/): 存放Nginx的主配置文件nginx.conf以及其他配置文件。这个目次通常包括:
直接从Docker Hub拉取Nginx镜像拉取最新的Nginx官方镜像: [code]docker pull nginx [/code]如果你想要拉取特定版本的Nginx镜像,可以通过指定标签来完成。比方,要拉取Nginx 1.19版本的镜像,可以使用: [code]docker pull nginx:1.19 [/code]检察已下载的镜像 [code]docker images [/code]配置Nginx /home/nginx/nginx.conf [code]worker_processes 1; # 工作历程数量,通常设置为CPU核心数 events { worker_connections 1024; # 每个工作历程的最大毗连数 } http { include /etc/nginx/mime.types; # 包含MIME类型界说 default_type application/octet-stream; # 默认文件类型 sendfile on; # 启用高效文件传输模式 keepalive_timeout 65; # 保持毗连的超时时间 server { listen 80; # 监听的端口 server_name localhost; # 处理以/开头的哀求 location / { root /usr/share/nginx/html; # 界说网站静态资源根目次 index index.html index.htm; # 默认文件(从root目次下找) } # 处理以/api/开头的哀求,并将它们转发到后端服务器 location /api/ { # 指令用于修改或添加传递给后端服务器的哀求头。 proxy_set_header Host $http_host; # 哀求头中的Host字段的值 proxy_set_header X-Real-IP $remote_addr; # 发起哀求的客户端的IP地址 proxy_set_header REMOTE-HOST $remote_addr; # 在署理哀求时构建X-Forwarded-For头(用于追踪哀求的原始IP地址,可包含多个) proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_pass 署理转发哀求的目标地址; # 后端地址 } error_page 500 502 503 504 /50x.html; # 错误页面 location = /50x.html { root /usr/share/nginx/html; # 错误页面的根目次 } } } [/code]/home/nginx/html/index.html [code] <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-inline' 'unsafe-eval';" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>my nginx test</title> </head> <body> <div>hello nginx html</div> <h1>edit html</h1> </body> </html>[/code]启动Nginx [code]docker run --name my-nginx-test3 -p 80:80 -v /home/nginx/nginx.conf:/etc/nginx/nginx.conf -v /home/nginx/html:/usr/share/nginx/html -d nginx [/code]启动容器:docker run 检察运行的容器 [code]docker ps [/code]运行后修改配置文件重启生效 重载Nginx配置: [code]docker exec <容器name> nginx -s reload [/code]
欣赏器输入ip:端口 通过Dockerfile构建Nginx镜像后拉取注意实行上下文路径 创建Dockerfile:创建一个Dockerfile来构建您的Nginx镜像。 [code]# 使用Nginx官方镜像作为根本镜像 FROM nginx:alpine # 将dist目次(包含index.html和其他静态文件)复制到Nginx容器的静态文件目次 # ./dist相对实行构建上下文的路径 COPY ./dist/ /usr/share/nginx/html/ # 将自界说的Nginx配置文件复制到Nginx容器(此步调将修改nginx完备的主配置文件) COPY nginx/nginx.conf /etc/nginx/nginx.conf [/code]
构建和运行Docker容器:使用Dockerfile和Nginx配置文件构建Docker镜像,并运行容器。 构建Docker镜像 [code]docker build -t my-dockerfile-nginx -f Dockerfile . [/code]-t: 选项用于指定镜像的名称和标签 运行Docker容器,开放9999端口访问 [code]docker run --name dockerfile-nginx -p 9999:80 -d my-dockerfile-nginx [/code]欣赏器输入ip:端口 到此这篇关于docker启动Nginx的两种方式小结的文章就介绍到这了,更多相关docker启动Nginx内容请搜索脚本之家以前的文章或继续欣赏下面的相关文章希望各人以后多多支持脚本之家! 来源:https://www.jb51.net/server/328772oy4.htm 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |
|手机版|小黑屋|梦想之都-俊月星空
( 粤ICP备18056059号 )|网站地图
GMT+8, 2025-7-1 19:44 , Processed in 0.038729 second(s), 18 queries .
Powered by Mxzdjyxk! X3.5
© 2001-2025 Discuz! Team.