使用HTTPS

前言

如果正常的话,我当前的域名应该是:https://hexo.zhywyt.me,为了庆祝我找到了免费的https方案,特地来写下这篇文章。

话不多说展示军火:

1
2
3
4
- frp
- nginx
- acme.sh
- ZeroSSL/Let's Encrypt

具体思路是,使用nginx做本地的两个服务,一个是81端口的http转https服务,一个是正常的80端口http服务。再使用acme.shZeroSSL或者Let's Encrypt自动申请证书,然后使用frp为本地 HTTP 服务启用 HTTPS | frp

然而你也可以不使用frphttps服务,那么你需要使用nginx进行认证,你可以参考:免费永久HTTPS(ssl)证书——Let’s Encrypt来了 - 大司徒 - 博客园

让我们开始HTTPS

frps

你需要启用frpshttps穿透,具体配置文件为:

1
2
3
4
5
6
7
8
9
bindPort = 7000
vhostHTTPPort = 80
vhostHTTPSPort = 443
webServer.addr = "0.0.0.0"
webServer.port = 7500
webServer.user = "****"
webServer.password = "****"
enablePrometheus = true
auth.token = "****"

重要的是你的vhostHTTPSPort字段,其他的请不要修改。修改完配置后重启frps,并打开你服务器的443端口。

nginx

为什么使用nginx因为我只会用这个,并且它的配置文件很有意思,且好玩。

使用frp

如果你使用了frp,那么你就不需要在nginx中设置443端口的https服务,只需要开放本机上的http服务即可。请修改下面的<yourservername>为你想要的名字。

1
2
cp /etc/nginx/sites-available/default /etc/nginx/sites-available/<yourservername>
ln -s /etc/nginx/sites-available/<yourservername> /etc/nginx/sites-enabled/<yourservername>

并将下面的配置文件写入你创建的文件中。请修改下面的<your server path>为你的网站路径,修改下面的<your domain>为你的域名。

1
2
3
4
5
6
7
8
9
10
server{
listen 81;
rewrite ^(.*)$ https://$host$1 permanent;
}
server {
listen 80;
root <your server path> ;
index index.html;
server_name <your domain> ;
}

然后重启nginx

1
sudo systemctl restart nginx

acme.sh

我使用了acme.sh项目来实现证书的申请和自动更新!🥰
这是令人兴奋的,但是我有义务让你知道免费SSL证书的来源!我是从这篇知乎文章得知了这一渠道:为什么 SSL 证书要花钱购买,而不是政府免费发放的?怎么知道卖证书的是否可信呢?

接下来,让我们开始吧!

1
2
# 安装依赖
sudo apt update && sudo apt install unzip socat

然后你需要准备一个邮箱,并尝试运行下面的命令来下载acme.sh

1
2
curl https://get.acme.sh | sh -s email=my@example.com
source ~/.bashrc

使用frp

接下来你需要准备:

  • 申请证书的域名
  • 服务的根目录
    并填入下面的命令,尝试执行它。
1
acme.sh --issue -d mydomain.com -d www.mydomain.com --webroot /home/wwwroot/mydomain.com/

然后你需要选择两个路径位置,用于存档certkey,你可以直接使用下面命令中的路径,否则你应该记住自己使用的路径,你应该修改下面的域名为你自己设置的域名

1
2
3
4
5
mkdir /etc/nginx/ssl
acme.sh --install-cert -d example.com \
--key-file /etc/nginx/ssl/key.pem \
--fullchain-file /etc/nginx/ssl/cert.pem \
--reloadcmd "service nginx reload"

后续

到这里你可以使用下面的命令来查看是否成功安装证书了!

1
acme.sh --info -d example.com

并且你可以查看自己的定时任务:

1
crontab -e

上线网站

对于不使用frp的用户,其实重启nginx就可以正常工作了,但是对于frp用户,还需要进行一些frpc的配置。

打开你的frpc.toml,输入下面的配置:
你可以参考:为本地 HTTP 服务启用 HTTPS | frp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
serverAddr = "x.x.x.x"
serverPort = 7000

[[proxies]]
name = "test_htts2http"
type = "https"
customDomains = ["domain"]

[proxies.plugin]
type = "https2http"
localAddr = "127.0.0.1:80"

# HTTPS 证书相关的配置
crtPath = "./server.crt"
keyPath = "./server.key"
hostHeaderRewrite = "127.0.0.1"
requestHeaders.set.x-from-where = "frp"

并额外添加一个http的服务:

1
2
3
4
5
6
[[proxies]]
name = "httpname"
type = "http"
localIP = "localhost"
localPort = 81
customDomains = ["domain"]

最后重启frpc!大功告成!👋🛠️

如果有任何问题,欢迎向我提问!


使用HTTPS
http://hexo.zhywyt.me/posts/usehttps/
作者
zhywyt
发布于
2025年3月14日
更新于
2025年3月17日
许可协议