Docker版单容器Nginx运行多web项目

1
2
3
4
5
6
作者: 夜泊1990
企鹅: 1611756908
Q 群: 948233848
邮箱: hd1611756908@163.com
博客: https://hs-an-yue.github.io/
B 站: https://space.bilibili.com/514155929/

第一章 需求介绍

1
2
3
1. 想使用docker运行一个nginx容器
2. 公司有多个前端项目需要nginx运行
3. 想将多个项目放在同一个nginx中,让一个nginx容器同时运行多个前端项目

第二章 实现步骤

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
第一步: 以两个项目为例: 
1. 项目1为: admin
2. 项目二为:rag

第二步: 容器nginx的本机挂载卷创建,我将容器nginx的挂载卷,挂载到了 /home/nginx 跟目录下

1. 创建容器涉及到的目录: mkdir -p /home/nginx/{conf.d,html/admin,html/rag,ssl,logs}

目录1: /home/nginx/conf.d 此目录映射nginx容器default.conf配置文件
目录2: /home/nginx/html/admin 此目录映射nginx容器admin项目的前端文件
目录3: /home/nginx/html/rag 此目录映射nginx容器rag项目的前端文件
目录3: /home/nginx/ssl 此目录映射nginx容器的https证书目录
目录4: /home/nginx/logs 此目录映射nginx容器运行的日志

2. 配置文件: nginx有两个配置文件分别为 nginx.conf和default.conf
nginx.conf配置文件放到/home/nginx目录下
default.conf配置文件放到/home/nginx/conf.d目录下

3. 执行Docker命令启动nginx容器

docker run -d \
--name nginx \
--restart always \
-p 80:80 \
-p 443:443 \
-v /home/hs/nginx/nginx.conf:/etc/nginx/nginx.conf \
-v /home/hs/nginx/conf.d:/etc/nginx/conf.d \
-v /home/hs/nginx/html:/usr/share/nginx/html \
-v /home/hs/nginx/ssl:/etc/nginx/ssl \
-v /home/hs/nginx/logs:/var/log/nginx \
nginx:latest

第三章 nginx配置文件详细介绍

1. nginx.conf配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
user  nginx; 						# 指定 Nginx  worker 子进程运行的用户,不要用root
worker_processes auto; # 指定 Nginx 创建的 worker 工作进程数量,设置为 auto 会让 Nginx 自动检测服务器的 CPU 核心数,并启动相同数量的进程

error_log /var/log/nginx/error.log notice; # 指定全局错误日志文件的存放路径与日志记录级别,/var/log/nginx/路径为容器内路径,不要修改,notice为日志级别(高于 debug/info,低于 warn/error)
pid /run/nginx.pid; # 指定存放 Nginx 主进程 PID(进程 ID)文件的路径


events {
worker_connections 1024; # 每个 worker 进程允许的最大并发连接数;理论上该 Nginx 实例能处理的最大并发连接数为 worker_processes * worker_connections(当前配置下,若为 4 核 CPU,则最大连接数为 4 ✖ 1024 = 4096)
}


http {
include /etc/nginx/mime.types; # 告诉 Nginx 如何根据文件后缀名设置 HTTP 响应头中的 Content-Type(例如让浏览器知道 .html 是网页,.png 是图片
default_type application/octet-stream; # 默认的 MIME 类型(二进制流):当文件后缀未在 mime.types 中定义时,默认使用此类型,提示浏览器“将其作为二进制文件直接下载”
# 日志输出格式模板
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; # 开启高效文件传输模式

keepalive_timeout 65; # HTTP 保持长连接(Keep-Alive)的超时时间,单位为秒;客户端与服务器建立 TCP 连接后,若 65 秒内没有新的请求,Nginx 会主动断开该连接,防止连接被无限制占用

# 隐藏nginx版本
server_tokens off;

#gzip on; # 开启 Gzip 传输压缩功能;Nginx 会在传输文本类文件(如 HTML、CSS、JS)前进行压缩,可显著节省网络带宽并加快网页加载 speed

include /etc/nginx/conf.d/*.conf; # 包含并加载指定目录下的所有以 .conf 结尾的子配置文件,不要修改 /etc/nginx/conf.d/ 路径,此路径为容器内路径
}

2. default.conf配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
server {
listen 80; # 监听 IPv4 的 80 端口(标准 HTTP 端口)
listen [::]:80; # 监听 IPv6 的 80 端口
server_name 106.14.27.178; # 匹配的域名或 IP 地址


# ==========================
# 首页
# =========================
location / {
root /usr/share/nginx/html; # 指定网站首页的根目录,容器内的路径,不能用户自己宿主机的路径
index index.html;
try_files $uri $uri/ /index.html;
}



# =========================
# admin管理后台
# =========================

location /admin/ {
alias /usr/share/nginx/html/admin/; # # 指定网页根目录,容器内的路径,不能用户自己宿主机的路径
index index.html;
try_files $uri $uri/ /admin/index.html;
}

# =========================
# RAG前端
# =========================

location /rag/ {
alias /usr/share/nginx/html/rag/; # # 指定网页根目录,容器内的路径,不能用户自己宿主机的路径
index index.html;
try_files $uri $uri/ /rag/index.html;
}

location /api/ {
# 后端服务地址
proxy_pass http://106.14.27.178:8080/;

# 代理请求头,传递真实客户端IP
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 X-Forwarded-Proto $scheme;
# websocket支持(RAG流式输出可能需要)
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_read_timeout 300s;
}

#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /home/hs/nginx/html;
}
}


每走一步,都是进步.

--------------------------已经到底啦!--------------------------