首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >续费TLS证书后如何自动重启Nginx docker容器?

续费TLS证书后如何自动重启Nginx docker容器?
EN

Stack Overflow用户
提问于 2021-03-15 20:40:01
回答 1查看 536关注 0票数 1

我的应用程序有一个Nginx容器。这就是docker-compose中定义的服务。

代码语言:javascript
复制
  nginx:
    image: nginx:latest
    restart: always
    volumes:
    - ./nginx/:/etc/nginx/
    ports:
      - 80:80
      - 443:443

卷nginx目录包含配置文件和certs文件夹。

在部署应用程序的主机中,使用以下命令手动生成TLS证书:

代码语言:javascript
复制
certbot certonly --manual --manual-public-ip-logging-ok --preferred-challenges dns -d my.app.com

并且存在从实际letsEncrypt证书位置到此主机卷位置的符号链接。

代码语言:javascript
复制
/myapp/nginx/certs# ll

lrwxrwxrwx 1 root root   55 Mar 12 09:47 fullchain.pem -> /etc/letsencrypt/live/my.app.com/fullchain.pem
lrwxrwxrwx 1 root root   53 Mar 12 09:48 privkey.pem -> /etc/letsencrypt/live/my.app.com/privkey.pem

每次证书过期时,我都会重新生成证书。(由于证书的生成是手动的,所以我不能使用certbot更新命令进行自动续订。当我尝试这样做时,它会给出一个错误:)

代码语言:javascript
复制
Failed to renew certificate my.app.com with error: The manual plugin is not working; there may be problems with your existing configuration.
The error was: PluginError('An authentication script must be provided with --manual-auth-hook when using the manual plugin non-interactively.') 

因此,当重新生成时,Certbot将续订/etc/letsencrypt/live/my.app.com/目录中的证书)。在那之后,我必须重新启动nginx容器来获取新的证书。

有没有一种方法可以自动做到这一点?有没有什么钩子可以连接到certbot来做到这一点?

EN

回答 1

Stack Overflow用户

发布于 2021-03-15 22:11:20

以下是如何使用md5测试证书是否更改的方法:

代码语言:javascript
复制
file=/path/to/the/certificate ; \
  before=$(md5sum "$file") ; \
  certbot ... ; \
  after=$(md5sum "$file"); \
  test "$before" = "$after" || docker exec ...

这里发生了什么:

序列将当前文件的校验和保存到一个名为before.

  • Then的变量中进行更新尝试(不要忘记添加实际的command).

  • After更新证书校验和被保存到

  • md5 after中)比较两个字符串,如果它们不匹配,此命令退出,状态码为1。如果它们确实匹配,则退出代码为0.

  • ||执行它之后的任何内容,如果上一个命令退出时不是0,则退出。在这种情况下,当校验和不匹配时。
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66638205

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档