Docker安装Nginx
Xplorist Lv6

Docker安装Nginx

reference-site-list

steps

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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
cd /data
mkdir nginx

docker pull nginx:latest

docker run -d --name nginx nginx:latest

docker cp nginx:/usr/share/nginx/html /data/nginx

docker cp nginx:/etc/nginx /data/nginx

mv /data/nginx/nginx /data/nginx/config

# 创建证书文件夹
cd /data/nginx/config
mkdir certificate
cd certificate
mkdir xplorist.tech

vi /data/nginx/config/nginx.conf
## 对nginx.conf进行编辑
#/data/nginx/config/nginx.conf

#include /etc/nginx/conf.d/*.conf;
include /etc/nginx/conf.d/xplorist.tech.conf

## nginx.conf编辑结束

vi /data/nginx/config/conf.d/xplorist.tech.conf
## 新增xplorist.tech.conf
#/data/nginx/config/conf.d/xplorist.tech.conf
server {
listen 80;
server_name xplorist.tech;

#80跳转到443
rewrite ^(.*)$ https://${server_name}$1 permanent;

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}

server {
listen 443 ssl http2;
server_name xplorist.tech;

ssl_certificate /etc/nginx/certificate/xplorist.tech/full.pem;
ssl_certificate_key /etc/nginx/certificate/xplorist.tech/key.pem;

ssl_session_timeout 5m;

#开启HSTS
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;

ssl_ciphers ECDHE-RSA-AES256-GCM-SHA512:DHE-RSA-AES256-GCM-SHA512:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384;
#适时移除TLSv1.2
ssl_protocols TLSv1.3 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;

location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
## 对default.conf修改结束

docker stop nginx

docker rm nginx

docker run -d --name nginx --restart=always --label=sh.acme.autoload.domain=xplorist.tech -v /data/nginx/html:/usr/share/nginx/html -v /data/nginx/config:/etc/nginx -p 80:80 -p 443:443 nginx:latest

使用acme.sh申请证书

  1. 向阿里云获取AccessKey

阿里云获取AccessKey

  1. 首先创建acme.sh容器
1
2
3
4
5
6
7
8
9
10

# 使用守护进程的方式启动容器
docker run --rm -itd \
-v "$(pwd)/out":/acme.sh \
--net=host \
--name=acme.sh \
-v /var/run/docker.sock:/var/run/docker.sock \
-e Ali_Key="erqwetqrewer" \
-e Ali_Secret="tyretuetuert" \
neilpang/acme.sh daemon
  1. 使用acme.sh申请证书
1
2
3
4
# 颁发证书
docker exec -it \
acme.sh --issue --dns dns_ali -d xplorist.tech -d "*.xplorist.tech" \
--server letsencrypt
  1. 使用acme.sh自动部署证书

    1
    2
    3
    4
    5
    6
    7
    8
    docker exec \
    -e DEPLOY_DOCKER_CONTAINER_LABEL=sh.acme.autoload.domain=xplorist.tech \
    -e DEPLOY_DOCKER_CONTAINER_KEY_FILE=/etc/nginx/certificate/xplorist.tech/key.pem \
    -e DEPLOY_DOCKER_CONTAINER_CERT_FILE="/etc/nginx/certificate/xplorist.tech/cert.pem" \
    -e DEPLOY_DOCKER_CONTAINER_CA_FILE="/etc/nginx/certificate/xplorist.tech/ca.pem" \
    -e DEPLOY_DOCKER_CONTAINER_FULLCHAIN_FILE="/etc/nginx/certificate/xplorist.tech/full.pem" \
    -e DEPLOY_DOCKER_CONTAINER_RELOAD_CMD="service nginx force-reload" \
    acme.sh --deploy -d xplorist.tech --deploy-hook docker
  2. 检查网站证书是否有效

该证书有3个月的有效期,快到期时,acme.sh将自动重新申请并部署证书与重载Nginx。

更改宿主机上的配置文件后重载docker中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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
vi /data/nginx/config/nginx.conf
## 对nginx.conf进行编辑
#/data/nginx/config/nginx.conf

#include /etc/nginx/conf.d/*.conf;
include /etc/nginx/conf.d/xplorist.tech.conf

## nginx.conf编辑结束

vi /data/nginx/config/conf.d/xplorist.tech.conf
## 对xplorist.tech.conf进行编辑
#/data/nginx/config/conf.d/xplorist.tech.conf
server {
listen 80;
server_name xplorist.tech;

#80跳转到443
rewrite ^(.*)$ https://${server_name}$1 permanent;

error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
}

server {
listen 443 ssl http2;
server_name xplorist.tech;

ssl_certificate /etc/nginx/certificate/xplorist.tech/full.pem;
ssl_certificate_key /etc/nginx/certificate/xplorist.tech/key.pem;

ssl_session_timeout 5m;

#开启HSTS
add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;

ssl_ciphers ECDHE-RSA-AES256-GCM-SHA512:DHE-RSA-AES256-GCM-SHA512:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES256-GCM-SHA384;
#适时移除TLSv1.2
ssl_protocols TLSv1.3 TLSv1.2;
ssl_prefer_server_ciphers on;
ssl_session_cache shared:SSL:10m;

location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
}
## xplorist.tech.conf编辑结束


docker exec -it nginx /bin/bash

service nginx force-reload

exit

反向代理的重要研究

  • 只有反向代理是不够的,还要项目本身支持设置contextPath,如果项目本身不支持设置contextPath,前端路由到没有对应网关代理的路径然后就报404,想要实现Nginx通过一个contextPath实现整个项目由nginx的80/443端口反向代理去访问,必须要该项目支持修改contextPath上下文,如果是这样的话,就需要将docker中的项目配置文件映射到宿主机上。

  • docker容器有网络,看到一个例子没有使用nginx的反向代理,只是用了docker容器的网络特性,最后仔细研究了一下,也是用到了反向代理,只不过代理是由容器内部的网络访问的,而不是我通常使用的宿主机带端口方式去访问。

  • Nginx中使用https后,作反向代理会出现一些前端访问被拒的问题,暂时还有点疑惑,又貌似是WordPress设置的问题。

  • Nginx的/根进行反向代理,好像很容易出现问题,WordPress反向代理出问题估计就在这儿。

  • 其实docker也有它的缺点,并不能够完全取代宿主机安装软件,比如说宿主机的Nginx上可以同时安装多个php项目,通过项目文件内的配置来灵活处理ContextPath上下文。

  • 自己的Nginx反向代理的知识模型应该和Nginx的真实模型有点出入,该认真补一下Nginx官方的文档了,后面来认真学习一下Nginx。

  • 之前以为的Nginx + Docker的超强组合,现在好像有点动摇了,没有想象中的那么强,还是应该认真了解Nginx和Docker,多看看资料和官方文档,自己从实践中总结的知识模型有大部分应该都是猜想,还是应该回归正统的理论。

 评论