Skip to content

Commit

Permalink
fix: https 没有无法开启问题 #273
Browse files Browse the repository at this point in the history
  • Loading branch information
Mereithhh committed Apr 13, 2023
1 parent 4896a50 commit 3fad711
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 134 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"editor.defaultFormatter": "esbenp.prettier-vscode"
},
"[json]": {
"editor.defaultFormatter": "esbenp.prettier-vscode"
"editor.defaultFormatter": "vscode.json-language-features"
},
"eslint.validate": ["javascript", "typescript"],
"editor.codeActionsOnSave": {
Expand Down
111 changes: 0 additions & 111 deletions Caddyfile

This file was deleted.

8 changes: 5 additions & 3 deletions CaddyfileTemplate
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{
auto_https disable_redirects
on_demand_tls {
ask http://127.0.0.1:3000/api/admin/caddy/ask
}
Expand All @@ -8,6 +7,7 @@
log {
output file /var/log/caddy.log
}

}
(h) {
trusted_proxies private_ranges
Expand Down Expand Up @@ -106,7 +106,9 @@
reverse_proxy 127.0.0.1:3001
}

:80, :443 {
http:// {
import van
}
https:// {
import van
}

2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ ENV VAN_BLOG_WALINE_DB "waline"
# 复制静态文件
WORKDIR /app/admin
COPY --from=ADMIN_BUILDER /app/dist/ ./
COPY CaddyfileTemplate /app/CaddyfileTemplate
COPY caddyTemplate.json /app/caddyTemplate.json
# 复制入口文件
WORKDIR /app
COPY ./entrypoint.sh ./
Expand Down
1 change: 1 addition & 0 deletions caddyTemplate.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"admin":{"listen":"0.0.0.0:2019"},"logging":{"logs":{"default":{"writer":{"filename":"/var/log/caddy.log","output":"file"},"exclude":["http.log.access.log1","http.log.access.log0"]},"log0":{"writer":{"filename":"/var/log/vanblog-access.log","output":"file"},"encoder":{"format":"json"},"include":["http.log.access.log0"]},"log1":{"writer":{"filename":"/var/log/vanblog-access.log","output":"file"},"encoder":{"format":"json"},"include":["http.log.access.log1"]}}},"apps":{"http":{"servers":{"srv0":{"listen":[":443"],"routes":[{"handle":[{"encodings":{"gzip":{},"zstd":{}},"handler":"encode","prefer":["zstd","gzip"]}]},{"group":"group38","match":[{"path":["/sitemap.xml"]}],"handle":[{"handler":"subroute","routes":[{"handle":[{"handler":"rewrite","uri_substring":[{"find":"/sitemap.xml","replace":"/sitemap/sitemap.xml"}]},{"handler":"reverse_proxy","upstreams":[{"dial":"127.0.0.1:3000"}]}]}]}]},{"group":"group38","match":[{"path":["/api/comment"]}],"handle":[{"handler":"subroute","routes":[{"handle":[{"handler":"rewrite","strip_path_prefix":"/api"},{"handler":"reverse_proxy","trusted_proxies":["192.168.0.0/16","172.16.0.0/12","10.0.0.0/8","127.0.0.1/8","fd00::/8","::1"],"upstreams":[{"dial":"127.0.0.1:8360"}]}]}]}]},{"group":"group38","match":[{"path":["/feed.json"]}],"handle":[{"handler":"subroute","routes":[{"handle":[{"handler":"rewrite","uri_substring":[{"find":"/feed.json","replace":"/rss/feed.json"}]},{"handler":"reverse_proxy","upstreams":[{"dial":"127.0.0.1:3000"}]}]}]}]},{"group":"group38","match":[{"path":["/comment*"]}],"handle":[{"handler":"subroute","routes":[{"handle":[{"handler":"reverse_proxy","trusted_proxies":["192.168.0.0/16","172.16.0.0/12","10.0.0.0/8","127.0.0.1/8","fd00::/8","::1"],"upstreams":[{"dial":"127.0.0.1:8360"}]}]}]}]},{"group":"group38","match":[{"path":["/favicon*"]}],"handle":[{"handler":"subroute","routes":[{"handle":[{"handler":"rewrite","uri_substring":[{"find":"/favicon","replace":"/static/img/favicon"}]},{"handler":"reverse_proxy","upstreams":[{"dial":"127.0.0.1:3000"}]}]}]}]},{"group":"group38","match":[{"path":["/static/*"]}],"handle":[{"handler":"subroute","routes":[{"handle":[{"handler":"reverse_proxy","upstreams":[{"dial":"127.0.0.1:3000"}]}]}]}]},{"group":"group38","match":[{"path":["/custom/*"]}],"handle":[{"handler":"subroute","routes":[{"handle":[{"handler":"reverse_proxy","upstreams":[{"dial":"127.0.0.1:3000"}]}]}]}]},{"group":"group38","match":[{"path":["/feed.xml"]}],"handle":[{"handler":"subroute","routes":[{"handle":[{"handler":"rewrite","uri_substring":[{"find":"/feed.xml","replace":"/rss/feed.xml"}]},{"handler":"reverse_proxy","upstreams":[{"dial":"127.0.0.1:3000"}]}]}]}]},{"group":"group38","match":[{"path":["/atom.xml"]}],"handle":[{"handler":"subroute","routes":[{"handle":[{"handler":"rewrite","uri_substring":[{"find":"/feed.xml","replace":"/rss/atom.xml"}]},{"handler":"reverse_proxy","upstreams":[{"dial":"127.0.0.1:3000"}]}]}]}]},{"group":"group38","match":[{"path":["/swagger*"]}],"handle":[{"handler":"subroute","routes":[{"handle":[{"handler":"reverse_proxy","upstreams":[{"dial":"127.0.0.1:3000"}]}]}]}]},{"group":"group38","match":[{"path":["/token*"]}],"handle":[{"handler":"subroute","routes":[{"handle":[{"handler":"reverse_proxy","trusted_proxies":["192.168.0.0/16","172.16.0.0/12","10.0.0.0/8","127.0.0.1/8","fd00::/8","::1"],"upstreams":[{"dial":"127.0.0.1:8360"}]}]}]}]},{"group":"group38","match":[{"path":["/oauth*"]}],"handle":[{"handler":"subroute","routes":[{"handle":[{"handler":"reverse_proxy","trusted_proxies":["192.168.0.0/16","172.16.0.0/12","10.0.0.0/8","127.0.0.1/8","fd00::/8","::1"],"upstreams":[{"dial":"127.0.0.1:8360"}]}]}]}]},{"group":"group38","match":[{"path":["/admin*"]}],"handle":[{"handler":"subroute","routes":[{"handle":[{"handler":"rewrite","strip_path_prefix":"/admin"}]},{"handle":[{"handler":"vars","root":"/app/admin"}]},{"handle":[{"handler":"rewrite","uri":"{http.matchers.file.relative}"}],"match":[{"file":{"try_files":["{http.request.uri.path}","/index.html"]}}]},{"handle":[{"handler":"file_server","hide":["./CaddyfileTemplate"]}]}]}]},{"group":"group38","match":[{"path":["/user*"]}],"handle":[{"handler":"subroute","routes":[{"handle":[{"handler":"reverse_proxy","trusted_proxies":["192.168.0.0/16","172.16.0.0/12","10.0.0.0/8","127.0.0.1/8","fd00::/8","::1"],"upstreams":[{"dial":"127.0.0.1:8360"}]}]}]}]},{"group":"group38","match":[{"path":["/rss/*"]}],"handle":[{"handler":"subroute","routes":[{"handle":[{"handler":"reverse_proxy","upstreams":[{"dial":"127.0.0.1:3000"}]}]}]}]},{"group":"group38","match":[{"path":["/api/*"]}],"handle":[{"handler":"subroute","routes":[{"handle":[{"handler":"reverse_proxy","upstreams":[{"dial":"127.0.0.1:3000"}]}]}]}]},{"group":"group38","match":[{"path":["/ui*"]}],"handle":[{"handler":"subroute","routes":[{"handle":[{"handler":"reverse_proxy","trusted_proxies":["192.168.0.0/16","172.16.0.0/12","10.0.0.0/8","127.0.0.1/8","fd00::/8","::1"],"upstreams":[{"dial":"127.0.0.1:8360"}]}]}]}]},{"group":"group38","match":[{"path":["/db*"]}],"handle":[{"handler":"subroute","routes":[{"handle":[{"handler":"reverse_proxy","trusted_proxies":["192.168.0.0/16","172.16.0.0/12","10.0.0.0/8","127.0.0.1/8","fd00::/8","::1"],"upstreams":[{"dial":"127.0.0.1:8360"}]}]}]}]},{"group":"group38","match":[{"path":["/c/*"]}],"handle":[{"handler":"subroute","routes":[{"handle":[{"handler":"reverse_proxy","upstreams":[{"dial":"127.0.0.1:3000"}]}]}]}]},{"handle":[{"handler":"reverse_proxy","upstreams":[{"dial":"127.0.0.1:3001"}]}]}],"tls_connection_policies":[{}],"logs":{"default_logger_name":"log1"}},"srv1":{"listen":[":80"],"routes":[{"handle":[{"encodings":{"gzip":{},"zstd":{}},"handler":"encode","prefer":["zstd","gzip"]}]},{"group":"group40","match":[{"path":["/sitemap.xml"]}],"handle":[{"handler":"subroute","routes":[{"handle":[{"handler":"rewrite","uri_substring":[{"find":"/sitemap.xml","replace":"/sitemap/sitemap.xml"}]},{"handler":"reverse_proxy","upstreams":[{"dial":"127.0.0.1:3000"}]}]}]}]},{"group":"group40","match":[{"path":["/api/comment"]}],"handle":[{"handler":"subroute","routes":[{"handle":[{"handler":"rewrite","strip_path_prefix":"/api"},{"handler":"reverse_proxy","trusted_proxies":["192.168.0.0/16","172.16.0.0/12","10.0.0.0/8","127.0.0.1/8","fd00::/8","::1"],"upstreams":[{"dial":"127.0.0.1:8360"}]}]}]}]},{"group":"group40","match":[{"path":["/feed.json"]}],"handle":[{"handler":"subroute","routes":[{"handle":[{"handler":"rewrite","uri_substring":[{"find":"/feed.json","replace":"/rss/feed.json"}]},{"handler":"reverse_proxy","upstreams":[{"dial":"127.0.0.1:3000"}]}]}]}]},{"group":"group40","match":[{"path":["/comment*"]}],"handle":[{"handler":"subroute","routes":[{"handle":[{"handler":"reverse_proxy","trusted_proxies":["192.168.0.0/16","172.16.0.0/12","10.0.0.0/8","127.0.0.1/8","fd00::/8","::1"],"upstreams":[{"dial":"127.0.0.1:8360"}]}]}]}]},{"group":"group40","match":[{"path":["/favicon*"]}],"handle":[{"handler":"subroute","routes":[{"handle":[{"handler":"rewrite","uri_substring":[{"find":"/favicon","replace":"/static/img/favicon"}]},{"handler":"reverse_proxy","upstreams":[{"dial":"127.0.0.1:3000"}]}]}]}]},{"group":"group40","match":[{"path":["/static/*"]}],"handle":[{"handler":"subroute","routes":[{"handle":[{"handler":"reverse_proxy","upstreams":[{"dial":"127.0.0.1:3000"}]}]}]}]},{"group":"group40","match":[{"path":["/custom/*"]}],"handle":[{"handler":"subroute","routes":[{"handle":[{"handler":"reverse_proxy","upstreams":[{"dial":"127.0.0.1:3000"}]}]}]}]},{"group":"group40","match":[{"path":["/feed.xml"]}],"handle":[{"handler":"subroute","routes":[{"handle":[{"handler":"rewrite","uri_substring":[{"find":"/feed.xml","replace":"/rss/feed.xml"}]},{"handler":"reverse_proxy","upstreams":[{"dial":"127.0.0.1:3000"}]}]}]}]},{"group":"group40","match":[{"path":["/atom.xml"]}],"handle":[{"handler":"subroute","routes":[{"handle":[{"handler":"rewrite","uri_substring":[{"find":"/feed.xml","replace":"/rss/atom.xml"}]},{"handler":"reverse_proxy","upstreams":[{"dial":"127.0.0.1:3000"}]}]}]}]},{"group":"group40","match":[{"path":["/swagger*"]}],"handle":[{"handler":"subroute","routes":[{"handle":[{"handler":"reverse_proxy","upstreams":[{"dial":"127.0.0.1:3000"}]}]}]}]},{"group":"group40","match":[{"path":["/token*"]}],"handle":[{"handler":"subroute","routes":[{"handle":[{"handler":"reverse_proxy","trusted_proxies":["192.168.0.0/16","172.16.0.0/12","10.0.0.0/8","127.0.0.1/8","fd00::/8","::1"],"upstreams":[{"dial":"127.0.0.1:8360"}]}]}]}]},{"group":"group40","match":[{"path":["/oauth*"]}],"handle":[{"handler":"subroute","routes":[{"handle":[{"handler":"reverse_proxy","trusted_proxies":["192.168.0.0/16","172.16.0.0/12","10.0.0.0/8","127.0.0.1/8","fd00::/8","::1"],"upstreams":[{"dial":"127.0.0.1:8360"}]}]}]}]},{"group":"group40","match":[{"path":["/admin*"]}],"handle":[{"handler":"subroute","routes":[{"handle":[{"handler":"rewrite","strip_path_prefix":"/admin"}]},{"handle":[{"handler":"vars","root":"/app/admin"}]},{"handle":[{"handler":"rewrite","uri":"{http.matchers.file.relative}"}],"match":[{"file":{"try_files":["{http.request.uri.path}","/index.html"]}}]},{"handle":[{"handler":"file_server","hide":["./CaddyfileTemplate"]}]}]}]},{"group":"group40","match":[{"path":["/user*"]}],"handle":[{"handler":"subroute","routes":[{"handle":[{"handler":"reverse_proxy","trusted_proxies":["192.168.0.0/16","172.16.0.0/12","10.0.0.0/8","127.0.0.1/8","fd00::/8","::1"],"upstreams":[{"dial":"127.0.0.1:8360"}]}]}]}]},{"group":"group40","match":[{"path":["/rss/*"]}],"handle":[{"handler":"subroute","routes":[{"handle":[{"handler":"reverse_proxy","upstreams":[{"dial":"127.0.0.1:3000"}]}]}]}]},{"group":"group40","match":[{"path":["/api/*"]}],"handle":[{"handler":"subroute","routes":[{"handle":[{"handler":"reverse_proxy","upstreams":[{"dial":"127.0.0.1:3000"}]}]}]}]},{"group":"group40","match":[{"path":["/ui*"]}],"handle":[{"handler":"subroute","routes":[{"handle":[{"handler":"reverse_proxy","trusted_proxies":["192.168.0.0/16","172.16.0.0/12","10.0.0.0/8","127.0.0.1/8","fd00::/8","::1"],"upstreams":[{"dial":"127.0.0.1:8360"}]}]}]}]},{"group":"group40","match":[{"path":["/db*"]}],"handle":[{"handler":"subroute","routes":[{"handle":[{"handler":"reverse_proxy","trusted_proxies":["192.168.0.0/16","172.16.0.0/12","10.0.0.0/8","127.0.0.1/8","fd00::/8","::1"],"upstreams":[{"dial":"127.0.0.1:8360"}]}]}]}]},{"group":"group40","match":[{"path":["/c/*"]}],"handle":[{"handler":"subroute","routes":[{"handle":[{"handler":"reverse_proxy","upstreams":[{"dial":"127.0.0.1:3000"}]}]}]}]},{"handle":[{"handler":"reverse_proxy","upstreams":[{"dial":"127.0.0.1:3001"}]}]}],"logs":{"default_logger_name":"log0"}}}},"tls":{"automation":{"policies":[{"issuers":[{"email":"VAN_BLOG_EMAIL","module":"acme"},{"email":"VAN_BLOG_EMAIL","module":"zerossl"}],"on_demand":true}],"on_demand":{"ask":"http://127.0.0.1:3000/api/admin/caddy/ask"}}}}}
2 changes: 1 addition & 1 deletion docs/advanced/https.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: HTTPS
icon: certificate
order: -3
order: 1
redirectFrom:
- /guide/https.html
- /feature/advance/https.html
Expand Down
17 changes: 15 additions & 2 deletions docs/guide/init.snippet.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,20 @@

部署 VanBlog 后,请使用浏览器打开 `http://<你的域名>/admin/init`,并按照指引完成初始化。具体设置项可以参考 [站点配置](/features/config.md)

::: note
你也可以直接访问博客地址,并点击导航栏右上角管理员按钮进入后台初始化页面。


::: tip

如果你直接使用默认的 80 和 443 端口,并且不打算套反代,

也可以直接访问博客地址,并点击导航栏右上角管理员按钮进入后台初始化页面。
直接访问 `https://<你的域名>/admin/init`,VanBlog 会自动为你访问的域名签发证书,并开启 HTTPS。

这个过程可能会比较慢,取决于域名解析验证的时长。

具体请参考 [HTTPS](/advanced/https.md)

如果需要套反向代理,请参考 [反代](/reference/reverse-proxy.md)

:::

4 changes: 2 additions & 2 deletions entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ echo "Version(Env): ${VAN_BLOG_VERSION}"
echo "============================================="


sed "s/VAN_BLOG_EMAIL/${EMAIL}/g" /app/CaddyfileTemplate >/app/Caddyfile
caddy start --config /app/Caddyfile
sed "s/VAN_BLOG_EMAIL/${EMAIL}/g" /app/caddyTemplate.json >/app/caddy.json
caddy start --config /app/caddy.json

cd /app/server && node main.js
22 changes: 9 additions & 13 deletions packages/server/src/provider/caddy/caddy.provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export class CaddyProvider {
await this.setRedirect(true);
txt = txt + 'https 自动重定向已开启';
} else {
await this.setRedirect(false);
txt = 'https 自动重定向已关闭';
}

Expand All @@ -39,15 +40,9 @@ export class CaddyProvider {
async setRedirect(redirect: boolean) {
if (!redirect) {
try {
await axios.post(
'http://127.0.0.1:2019/config/apps/http/servers/srv0/automatic_https',
{ disable_redirects: true },
);
await axios.post(
'http://127.0.0.1:2019/config/apps/http/servers/srv1/automatic_https',
{ disable_redirects: true },
await axios.delete(
'http://127.0.0.1:2019/config/apps/http/servers/srv1/listener_wrappers',
);

this.logger.log('https 自动重定向已关闭');
return '关闭成功!';
} catch (err) {
Expand All @@ -57,11 +52,12 @@ export class CaddyProvider {
}
} else {
try {
await axios.delete(
'http://127.0.0.1:2019/config/apps/http/servers/srv0/automatic_https',
);
await axios.delete(
'http://127.0.0.1:2019/config/apps/http/servers/srv1/automatic_https',
await axios.post(
'http://127.0.0.1:2019/config/apps/http/servers/srv1/listener_wrappers',[
{
"wrapper": "http_redirect"
}
]
);
this.logger.log('https 自动重定向已关闭');
return '开启成功!';
Expand Down

0 comments on commit 3fad711

Please sign in to comment.