麦克斯仇
Think different
159
文章
36871
阅读
首页
INDEX
文章
ARTICLE
关于
ABOUT
Docker 环境安装 Nginx
创建日期:
2020/02/11
修改日期:
2023/07/17
Docker
Nginx
> 以 `nginx:1.22.0` 为例 仓库地址以及教程:[https://hub.docker.com/_/nginx](https://hub.docker.com/_/nginx) ## 拉取 ```bash docker pull nginx:1.22.0 ``` ## 简单的使用示例 #### 测试demo ```bash # 启动容器 docker run -d --name nginx -p 8080:80 nginx:1.22.0 # 查看网页 curl http://127.0.0.1:8080 ``` #### 使用自定义卷部署简单的静态网页 ```bash docker run -d -v /some/content:/usr/share/nginx/html nginx:1.22.0 ``` #### 使用Dockerfile部署简单应用 Dockerfile: ```conf FROM nginx:1.22.0 ENV TZ=Asia/Shanghai COPY static-html-directory /usr/share/nginx/html ``` ```bash # 制作镜像 docker build -t mynginx . # 启动容器 docker run -d mynginx ``` ## 自定义Nginx配置文件 容器内的默认配置文件在`/etc/nginx/nginx.conf`,内容如下: ```bash user nginx; worker_processes 1; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/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 /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; #gzip on; include /etc/nginx/conf.d/*.conf; } ``` #### 使用自定义卷修改配置文件 ```bash docker run -d -v /host/path/nginx.conf:/etc/nginx/nginx.conf nginx:1.22.0 ``` #### 使用Dockerfile修改配置文件 Dockerfile: ```bash FROM nginx:1.22.0 COPY nginx.conf /etc/nginx/nginx.conf ``` ```bash # 制作镜像 docker build -t mynginx . # 启动容器 docker run -d mynginx ``` ## 个人推荐实战 > 以vue项目为例 vue项目中新建`nginx.conf`,文件内容如下: ```bash user nginx; worker_processes 1; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; } http { include /etc/nginx/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 /var/log/nginx/access.log main; sendfile on; #tcp_nopush on; keepalive_timeout 65; #gzip on; server { listen 80; proxy_set_header X-Real_IP $remote_addr; proxy_set_header Host $host; proxy_set_header X_Forward_For $proxy_add_x_forwarded_for; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; client_max_body_size 10M; location / { root /dist; try_files $uri $uri/ /index.html; } } } ``` vue项目中新建`Dockerfile`,内容如下 ```bash FROM nginx:1.22.0 ENV TZ=Asia/Shanghai COPY nginx.conf /etc/nginx/nginx.conf COPY dist/ /dist/ ``` 镜像制作与启动如下 ```bash # 制作镜像 docker build -t myweb . # 启动容器 docker run -d --name myweb -p 80:80 myweb ```
21
全部评论