在部署一个openlist的drive.yukifuhane.cn.eu.org后想要在同一台机子部署一个VPN域名为los.yukifuhane.dpdns.org但因为80和443端口已被占用,需要直连或者反代签证,我这边是设置请求8080端口,通过反代文件验证达到签证证书。网上没有这块太多的教程,也踩了不少坑

安装acme和nginx

1
2
3
4
5
6
$ apt install nginx
# 设置开机自启动
$ systemctl enbale nginx
# 下载acme与创建符号链接
$ curl https://get.acme.sh | sh
$ ln -s /root/.acme.sh/acme.sh /usr/local/bin/acme.sh

先让我们进行域名签证

1
2
3
4
5
6
7
$ acme.sh --set-default-ca --server letsencrypt
# Openlist
# 创建证书存放位置
$ mkdir /etc/drive
$ acme.sh --issue -d drive.yukifuhane.cn.eu.org -k ec-256 --webroot /var/www/html
$ acme.sh --install-cert -d drive.yukifuhane.cn.eu.org --ecc --key-file /etc/drive/server.key --fullchain-file /etc/drive/server.crt --reloadcmd "systemctl force-reload nginx"
$ docker run -itd --name openlist -p 5244:5244 -p 5245:5245 -v /mnt/openlist/data:/opt/openlist/data -v /etc/drive:/mnt/drive openlist:latest

nginx相关配置

niginx安装会默认有个WebSite,在 /var/www/html我们则需要复制一份

1
2
3
# 创建VPN站点
# mkdir -p /var/www/vpn
cp -r /var/www/html /var/www/vpn

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
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# https://www.nginx.com/resources/wiki/start/
# https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
# https://wiki.debian.org/Nginx/DirectoryStructure
#
# In most cases, administrators will remove this file from sites-enabled/ and
# leave it as reference inside of sites-available where it will continue to be
# updated by the nginx packaging team.
#
# This file will automatically load configuration files provided by other
# applications, such as Drupal or Wordpress. These applications will be made
# available underneath a path with that package name, such as /drupal8.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

# Default server configuration
#
server {
listen 80 default_server;
listen [::]:80 default_server;

# SSL configuration
#
# listen 443 ssl default_server;
# listen [::]:443 ssl default_server;
#
# Note: You should disable gzip for SSL traffic.
# See: https://bugs.debian.org/773332
#
# Read up on ssl_ciphers to ensure a secure configuration.
# See: https://bugs.debian.org/765782
#
# Self signed certs generated by the ssl-cert package
# Don't use them in a production server!
#
# include snippets/snakeoil.conf;

root /var/www/html;

# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;

server_name _;

location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ =404;
}

# 添加下面这段将默认访问80端口的/.well-known/acme-challenge/路径重定向到http://127.0.0.1:8080端口下
###############################################################
location /.well-known/acme-challenge/ {
proxy_pass http://127.0.0.1:8080;
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;
}
###############################################################


# pass PHP scripts to FastCGI server
#
#location ~ \.php$ {
# include snippets/fastcgi-php.conf;
#
# # With php-fpm (or other unix sockets):
# fastcgi_pass unix:/run/php/php7.4-fpm.sock;
# # With php-cgi (or other tcp sockets):
# fastcgi_pass 127.0.0.1:9000;
#}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}


# Virtual Host configuration for example.com
#
# You can move that to a different file under sites-available/ and symlink that
# to sites-enabled/ to enable it.
#
#server {
# listen 80;
# listen [::]:80;
#
# server_name example.com;
#
# root /var/www/example.com;
# index index.html;
#
# location / {
# try_files $uri $uri/ =404;
# }
#}

conf.d下配置

1
2
3
4
5
6
7
8
9
10
11
12
# /etc/nginx/nginx.conf会默认导入在/etc/nginx/conf.d/下以'.conf'结尾的文件
# 将监听8080端口,接受80重定向过来的路径/.well-known/acme-challenge/,而/.well-known/acme-challenge/下的文件是letsencrypt的文件验证
server {
listen 8080;
server_name los.yukifuhane.dpdns.org;
root /var/www/vpn;

# 只处理 ACME challenge 请求
location /.well-known/acme-challenge/ {
try_files $uri =404;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 2个域名以上可以参考这个
location /.well-known/acme-challenge/ {

# 使用 try_files 来尝试文件路径
try_files /var/www/html$uri /var/www/panel$uri =404;

# 设置代理端口
set $proxy_port 8080;

# 如果文件在 /var/www/panel 下找到,则设置代理端口为 6080
if (-f /var/www/panel$uri) {
set $proxy_port 6080;
}

# 代理请求到对应端口
proxy_pass http://127.0.0.1:$proxy_port;

# 设置请求头信息
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;
}

这是一些示例

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
# ========= 直连静态文件模式(8080)=========
server {
listen 8080;
server_name los.yukifuhane.dpdns.org;

# 指向和 acme.sh --webroot 参数一致的目录
root /var/www/vpn;

# 只处理 ACME challenge 请求
location /.well-known/acme-challenge/ {
# 直接尝试读取本地文件,找不到就 404
try_files $uri =404;
}
}

# ========= 反代模式(供 80 端口正常站点使用)=========
server {
listen 80;
server_name los.yukifuhane.dpdns.org;

# 如果是 ACME 验证路径,直接用本地文件,不重定向到 HTTPS
location /.well-known/acme-challenge/ {
root /var/www/vpn;
try_files $uri =404;
}

# 其他普通请求才反代到 HTTPS 或后端
location / {
proxy_pass https://127.0.0.1:443; # 后端的 HTTPS 服务
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;
}
}

# ========= HTTPS 模式(正常业务服务)=========
server {
listen 443 ssl http2;
server_name los.yukifuhane.dpdns.org;

# 你的证书路径(等申请下来以后替换)
ssl_certificate /etc/nginx/ssl/los.yukifuhane.dpdns.org.fullchain.pem;
ssl_certificate_key /etc/nginx/ssl/los.yukifuhane.dpdns.org.key;

root /var/www/vpn;
index index.html;

location / {
try_files $uri $uri/ =404;
}
}

让我们接着配置对VPN的域名进行签证

1
2
3
4
5
# VPN
# 创建证书存放位置
$ mkdir /etc/vpn
$ acme.sh --issue -d los.yukifuhane.dpdns.org --httpport 8080 -k ec-256 --webroot /var/www/vpn --debug
$ acme.sh --install-cert -d los.yukifuhane.dpdns.org --ecc --key-file /etc/vpn/server.key --fullchain-file /etc/vpn/server.crt --reloadcmd "systemctl force-reload nginx"