Docker安装Nginx
环境准备
首先,确保你的系统已安装 Docker。
创建挂载目录
在安装和配置 Nginx 之前,首先需要创建一些目录,用于存放 Nginx 配置文件、网页文件、日志文件和 SSL 证书等。
mkdir -p /opt/nginx/{conf.d,html,logs,ssl}
目录路径 | 核心用途 |
---|---|
/opt/nginx/conf | 存放自定义配置文件(如虚拟主机配置) |
/opt/nginx/html | 网站静态资源(HTML/CSS/JS/图片) |
/opt/nginx/logs | 存储访问日志/错误日志(access.log, error.log) |
/opt/nginx/ssl | HTTPS证书文件(.crt, .key, .pem) |
编写 Nginx 配置文件
在 /opt/nginx 目录下创建一个自定义的 Nginx 配置文件,配置部署 vue3-element-admin 项目的前端应用,并代理后端 API:
cat > /opt/nginx/nginx.conf << EOF
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
# 开启gzip
gzip on;
gzip_http_version 1.1;
gzip_disable "MSIE [1-6]\.";
gzip_min_length 1k;
gzip_buffers 16 64k;
gzip_comp_level 5;
gzip_vary on;
gzip_types text/plain application/javascript text/css;
server {
listen 80;
server_name localhost;
location / {
# 指定前端静态资源路径
# 需将 vue3-element-admin 项目构建后的 dist 目录拷贝至 /usr/share/nginx/html/vue
root /usr/share/nginx/html/vue;
index index.html index.htm;
}
# 代理后端 API 请求
location /prod-api/ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# 后端服务地址,注意保留 URL 末尾的 `/`
proxy_pass https://api.nutcracker.com/;
}
}
}
EOF
拉取 Nginx 容器
docker pull m.daocloud.io/docker.io/library/nginx:latest
注:docker pull nginx:latest
直接拉不下来,原因是docker.io被墙了,借道daocloud
拉取镜像。
启动 Nginx 容器
使用以下命令在 Docker 中启动 Nginx 容器,并挂载自定义配置文件和网页文件:
docker run -it -d --restart always --network host \
--name nginx \
-v /opt/nginx/nginx.conf:/etc/nginx/nginx.conf \
-v /opt/nginx/html:/usr/share/nginx/html \
-v /opt/nginx/logs:/var/log/nginx/ \
-v /opt/nginx/ssl:/etc/nginx/ssl \
m.daocloud.io/docker.io/library/nginx:latest
参数 | 功能说明 |
---|---|
–network host | 网络模式 |
–name nginx | 容器命名 |
-v [宿主机路径]:[容器路径] | 数据卷挂载 |
/opt/nginx/nginx.conf:/etc/nginx/nginx.conf | 挂载主配置文件 |
/opt/nginx/html:/usr/share/nginx/html | 挂载网站文件 |
/opt/nginx/logs:/var/log/nginx/ | 持久化存储日志 |
/opt/nginx/ssl:/etc/nginx/ssl | 挂载SSL证书 |
或者使用docker-compose来部署,先创建编写nginx-compose.yaml文件:
cat > /opt/nginx/nginx-compose.yaml << EOF
version: '2.0'
services:
nginx:
image: m.daocloud.io/docker.io/library/nginx:latest
container_name: nginx
restart: always
network_mode: "host"
volumes:
- /opt/nginx/nginx.conf:/etc/nginx/nginx.conf
- /opt/nginx/html:/usr/share/nginx/html
- /opt/nginx/logs:/var/log/nginx
- /opt/nginx/ssl:/etc/nginx/ssl
environment:
- TZ=Asia/Shanghai # 设置时区
mem_limit: 512m # 内存限制
EOF
docker-compose部署Nginx:
docker-compose -f /opt/nginx/nginx-compose.yaml up -d
验证安装
docker logs -f --tail 100 nginx
配置 SSL(可选)
如果您需要启用 HTTPS,可以通过 阿里云数字证书管理 申请免费的 SSL 证书。申请完成后,下载适用于 Nginx 服务器的证书文件。
下载后,解压并将 SSL 证书放置到 /opt/nginx/ssl 目录下。
接下来,在 Nginx 配置文件 nginx.conf 中添加以下配置来启用 HTTPS:
cat >
server {
listen 443 ssl;
server_name vue.nutcracker.com;
# 配置 SSL 证书路径
ssl_certificate ssl/vue.youlai.tech.pem;
ssl_certificate_key ssl/vue.youlai.tech.key;
location / {
root /usr/share/nginx/html/vue;
index index.html index.htm;
}
# 设置反向代理接口
location /prod-api/ {
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# 后端服务地址,注意保留 URL 末尾的 `/`
proxy_pass https://api.nutcracker.com/;
}
}
确保 SSL 证书的路径设置正确,并且 Nginx 配置无误。完成配置后,重新加载 Nginx 以使更改生效:
docker exec -it nginx nginx -s reload
引用Reference
- ELK安装