window系统docker版nginx建站


最近在一台window系统服务器上面折腾docker版nginx建站,发现host模式下浏览器无法直接访问页面,改为bridge模式又涉及容器间互相访问问题,最后通过把需要互相访问额容器放在同一个网络解决,本文以nginx和alist为例给大伙一点思路

创建外部网络

1
docker network create --driver bridge --subnet 172.23.0.0/16 my_web_networks

部署nginx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
services:
nginx:
image: nginx:latest
container_name: nginx
restart: unless-stopped
environment:
- TZ=Asia/Shanghai # 设置容器的时区为亚洲/上海
ports:
- 80:80
- 443:443
volumes:
- ./nginx.conf:/etc/nginx/nginx.conf
- ./conf.d:/etc/nginx/conf.d
- ./cert:/etc/nginx/cert
- ./log:/var/log/nginx
- ./html:/usr/share/nginx/html
- ./data:/data
networks:
my_web_networks:
ipv4_address: 172.23.0.2 # 替换为你希望的固定 IP 地址
networks:
my_web_networks:
external: true

部署alist

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
services:
alist:
restart: unless-stopped
volumes:
- ./data:/opt/alist/data
- /d:/data # 把D盘挂载为data目录
ports:
- 5244:5244
environment:
- TZ=Asia/Shanghai
container_name: alist
image: xhofe/alist:latest
networks:
my_web_networks:
ipv4_address: 172.23.0.3 # 替换为你希望的固定 IP 地址
networks:
my_web_networks:
external: true

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
32
33
34
35
// http
server {
listen 80 default_server;
listen [::]:80 default_server;
http2 on;
server_name default;
location / {
index index.html;
root /usr/share/nginx/html;
}
}
// https
server {
listen 443 ssl;
listen [::]:443 ssl;
http2 on;
server_name pan.example.com;
ssl_certificate "/etc/nginx/cert/example.com.pem";
ssl_certificate_key "/etc/nginx/cert/example.com.key";
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;
ssl_prefer_server_ciphers on;


location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
client_max_body_size 20000m;
proxy_pass http://172.23.0.3:5244; # nginx可访问地址
}
error_page 404 403 500 502 503 504 /404;
}

测试

  • 先访问http://localhost/测试nginx是否正常访问,注意要在html目录下放入html文件,否则会报错
  • 然后访问http://localhost:5244/测试alist是否正常访问
  • 最后做好域名解析后再访问https://pan.example.com看看是否正常,没有意外的话就一切正常,部署完毕