Osmanthus

空想具現化


  • 首页
  • 归档
  • 分类
  • 标签
  • 关于
  •   

© 2024 Homurax

UV: | PV:

Theme Typography by Makito

Proudly published with Hexo

acme 工具的使用与 cloudflare

发布于 2018-10-16 Server  acme nginx cloudflare 

博客目前使用的SSL证书是免费的Let’s Encrypt。
Let’s Encrypt的证书有效期是3个月,可以通过certbot renew来更新证书,但是只会更新还有30天才会过期的证书。

为了避免新添加域名,或是给证书更新时敲一堆命令,可以尝试使用Acme来申请、维护从letsencrypt生成的免费的证书.

项目地址 Neilpang/acme.sh

安装acme.sh

curl  https://get.acme.sh | sh

参照项目说明,普通用户和root用户都可以安装使用,它会把acme.sh安装到你的home目录下,并创建一个bash的alias, 方便你的使用。

我自己安装时发现并没有创建,如果没有创建的话,可以执行alias acme.sh=~/.acme.sh/acme.sh手动创建。

需要注意acme使用计划任务来自动更新证书,需要预先安装cron。

yum install -y cronie
systemctl enable crond
systemctl start crond

生成证书

acme.sh实现了acme协议支持的所有验证协议。一般有两种方式验证: http和dns验证。

各种方式参照项目的README.md即可,我使用的是dns的方式(cloudflare),acme.sh目前支持数十种解析商的自动集成。

export CF_Key="cloudflare中查看你的key"
export CF_Email="你的邮箱"

acme.sh  --issue  --dns dns_cf -d domain.com -d www.domain.com

# Let's Encrypt目前支持了通配符证书 所以也可以直接申请通配符证书
acme.sh  --issue  --dns dns_cf -d domain.com -d *.domain.com

安装证书

前面证书生成以后,接下来需要把证书copy到真正需要用它的地方。

注意,默认生成的证书都放在安装目录下~/.acme.sh/, 请不要直接使用此目录下的文件,例如: 不要直接让nginx/apache的配置文件使用这下面的文件。这里面的文件都是内部使用,而且目录结构可能会变化。

正确的使用方法是使用--installcert命令,并指定目标位置,然后证书文件会被copy到相应的位置。

# 例如位置是 /etc/nginx/ssl

acme.sh --install-cert -d domain.com \
--cert-file      /etc/nginx/ssl/cert.pem  \
--key-file       /etc/nginx/ssl/key.pem  \
--fullchain-file /etc/nginx/ssl/fullchain.pem


# copy之后修改你的nginx配置,添加上需要的信息,执行`nginx -t`检查配置无误后,再执行如下命令即可

acme.sh --install-cert -d domain.com \
--cert-file      /etc/nginx/ssl/cert.pem  \
--key-file       /etc/nginx/ssl/key.pem  \
--fullchain-file /etc/nginx/ssl/fullchain.pem  \
--reloadcmd   "systemctl force-reload nginx.service"

(一个小提醒,这里用的是service nginx force-reload,不是service nginx reload,据测试reload并不会重新加载证书,所以用的force-reload)

–installcert命令可以携带很多参数,来指定目标文件。并且可以指定reloadcmd,当证书更新以后,reloadcmd会被自动调用,让服务器生效

详细参数请参考: https://github.com/Neilpang/acme.sh#3-install-the-issued-cert-to-apachenginx-etc

值得注意的是,这里指定的所有参数都会被自动记录下来,并在将来证书自动更新以后,被再次自动调用

遇到过的问题

第一个问题是小问题,重启nginx服务时,提示了0.0.0.0:443被占用。回忆了半天才想起来以前用这个服务器跑了ss服务,居然用的是443端口!修改,重启之,解决。

第二个问题是访问站点时chrome提示了重定向过多,最后发现问题出在cloudflare的SSL策略上。

What SSL setting should I use?
This setting controls how Cloudflare’s servers connect to your origin for HTTPS requests. We recommend enabling the Full SSL (Strict) setting if possible. >Common use cases for each are:

Off: No visitors will be able to view your site over HTTPS; they will be redirected to HTTP.

Flexible SSL: You cannot configure HTTPS support on your origin, even with a certificate that is not valid for your site. Visitors will be able to access >your site over HTTPS, but connections to your origin will be made over HTTP. Note: You may encounter a redirect loop with some origin configurations.

Full SSL: Your origin supports HTTPS, but the certificate installed does not match your domain or is self-signed. Cloudflare will connect to your origin over >HTTPS, but will not validate the certificate.

Full (strict): Your origin has a valid certificate (not expired and signed by a trusted CA or Cloudflare Origin CA) installed. Cloudflare will connect over >HTTPS and verify the cert on each request.

原来设置如果是off的话,无论访问者采用http还是https来访问,cloudflare与你的服务器间通信是http。
设置成Flexible的话,你的网站不需要SSL证书,访问者与cloudflare通信是https,cloudflare与你的服务器间通信仍然是http。
Full与Full (strict)则为全程加密,区别在于Full可以使用自签证书,Full (strict)必须使用受信的SSL证书。

而我在nginx中配置了80跳443,所以如果设置成off或是Flexible,就会发生重定向过多的问题。设置成Full或是Full (strict)即可,cloudflare推荐的是设置成Full (strict)。

显示的证书

现在访问站点看到的SSL证书是cloudflare,如果想让访问者在浏览器中直接看到自己的证书(Let’s Encrypt),那需要升级为付费的 plan。

 上一篇: 建造者模式 下一篇: 全盘掌控堆空间的大小 

© 2024 Homurax

UV: | PV:

Theme Typography by Makito

Proudly published with Hexo