当前位置: 首页 > news >正文

Docker方式安装Nginx

安装Nginx

mkdir -p /data/docker/nginx/logs
mkdir -p /data/docker/nginx/html
docker run --network=host --restart=always --name nginx -d \
-v /data/docker/nginx/nginx.conf:/etc/nginx/nginx.conf:ro \
-v /data/docker/nginx/html:/etc/nginx/html \
-v /data/docker/nginx/logs:/var/log/nginx \
-v /data/app:/data/app \
nginx:latest

准备工作:创建目录

mkdir -p /data/docker/nginx/logs
mkdir -p /data/docker/nginx/html
​​mkdir -p​​:递归创建目录,如果父目录不存在则一并创建
​​/data/docker/nginx/logs​​:用于存储 Nginx 访问日志和错误日志的目录
​​/data/docker/nginx/html​​:用于存放网站静态文件(HTML/CSS/JS等)的目录
​​目的​​:在宿主机上创建持久化存储目录,确保容器重启后数据不丢失

Docker 运行命令详解

docker run \--network=host \               # 网络模式--restart=always \             # 重启策略--name nginx \                 # 容器命名-d \                           # 后台运行-v /data/docker/nginx/nginx.conf:/etc/nginx/nginx.conf:ro \  # 配置文件挂载-v /data/docker/nginx/html:/etc/nginx/html \                 # 网站文件挂载-v /data/docker/nginx/logs:/var/log/nginx \                  # 日志挂载-v /data/app:/data/app \                                     # 应用数据挂载nginx:latest                   # 使用最新版 Nginx 镜像1. ​​网络配置​​ --network=host
使用宿主机的网络命名空间
容器直接使用宿主机的 IP 地址和端口
​​优点​​:网络性能最好,无 NAT 转换损耗
​​缺点​​:端口冲突风险(容器直接使用主机端口)
2. ​​重启策略​​ --restart=always
容器退出时自动重启
包括 Docker 服务重启后自动启动容器
确保 Nginx 服务高可用
3. ​​容器命名​​ --name nginx
将容器命名为 "nginx"
方便后续管理:docker stop nginx/docker start nginx
4. ​​后台运行​​ -d
Detached 模式(后台运行)
终端释放,不阻塞当前会话

配置文件

配置文件路径/data/docker/nginx/nginx.conf

#user  nobody;
worker_processes 2;#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;#pid        logs/nginx.pid;events {worker_connections  1024;
}http {include       mime.types;default_type  application/octet-stream;#log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '#                  '$status $body_bytes_sent "$http_referer" '#                  '"$http_user_agent" "$http_x_forwarded_for"';#access_log  logs/access.log  main;sendfile        on;#tcp_nopush     on;#keepalive_timeout  0;keepalive_timeout  65;#gzip  on;server {listen       80;server_name  localhost;server_tokens off;client_max_body_size 5g;#charset koi8-r;#access_log  logs/host.access.log  main;location / {root   html;index  index.html index.htm;}location /model {add_header 'Access-Control-Allow-Headers' 'X-Requested-With';add_header 'Access-Control-Allow-Methods' 'GET,POST,OPTIONS';add_header 'Cross-Origin-Opener-Policy' 'same-origin';add_header 'Cross-Origin-Embedder-Policy' 'require-corp';add_header 'Access-Control-Allow-Origin' '*';add_header 'X-Frame-Options' 'ALLOWALL';alias html/model;index index.html index.htm;}location /api {proxy_pass http://localhost:8080;}location /psmap/web {alias /data/app/static/web;try_files $uri $uri/ /psmap/web/index.html;index index.html index.htm;}#error_page  404              /404.html;# redirect server error pages to the static page /50x.html#error_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}# proxy the PHP scripts to Apache listening on 127.0.0.1:80##location ~ \.php$ {#    proxy_pass   http://127.0.0.1;#}# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000##location ~ \.php$ {#    root           html;#    fastcgi_pass   127.0.0.1:9000;#    fastcgi_index  index.php;#    fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;#    include        fastcgi_params;#}# deny access to .htaccess files, if Apache's document root# concurs with nginx's one##location ~ /\.ht {#    deny  all;#}}
}
http://www.dtcms.com/a/528642.html

相关文章:

  • 标签噪声学习:理论与方法详解
  • Docker 部署 Debian 全流程教程
  • 上海做网站公司wordpress 活动网站
  • Bee:从 0 到 1 打造一套现代化的全栈后台管理系统(React + Spring Boot)
  • 计算机操作系统:“抖动”与工作集
  • 数据结构(长期更新)第4讲:单链表
  • C#测试调用OpenXml填充word文档的表格
  • 基于python的网站开发项目做外汇网站代理商
  • 对TCP/IP协议的理解
  • 如何判断“IP+端口“通不通
  • tensorrt c++部署
  • TypeScript 基础类型与接口详解
  • MySQL————mysql connect
  • 能打开各种网站的搜索引擎原神网页设计作业
  • 【SpringCloud】Ribbon(LoadBalancer ) 和 Feign
  • Dockerfile 中 ENTRYPOINT 和 CMD 有什么区别 ?
  • 网站数据库模版深圳网站建设黄浦网络 骗钱
  • vs code 下docker使用方法,以php 项目为示例
  • 番禺网站建设哪里好深圳十大传媒公司
  • 前端常见的设计模式
  • 亚马逊云渠道商:如何通过配置自动替换构建故障自愈的云架构?
  • 豆包 Python 和 Java 的 AI 集成及模型转换
  • 深入解析C++命令模式:设计原理与实际应用
  • 商城网站建设目标上海前十名文化传媒公司
  • ExpressionVisitor 的使用场景及方法
  • 《3D端游开放世界动态天气系统与场景交互优化实践日志》
  • Date类自主实现后的反思
  • 网络渗流:爆炸渗流
  • 购物网站建设款流程国外优秀设计网站大全
  • 【C++】STL容器-stack和queue的使用与模拟实现