在使用certbot为nginx获取新证书时出现404 Not Found错误

150次阅读
没有评论

问题描述

在尝试为服务器获取SSL证书时遇到了404 Not Found错误。他使用的是以下nginx配置:

server {
  server_name  www.nodrama.io;
  rewrite ^(.*) http://nodrama.io$1 permanent;
}

server {
  server_name  nodrama.io;
  listen 80;
  listen [::]:80 ipv6only=on;
  error_page 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 420 422 423 424 426 428 429 431 444 449 450 451 500 501 502 503 504 505 506 507 508 509 510 511 /X0X.html;
  location = /X0X.html {
    root /usr/share/nginx/html/;
    internal;
  }
  location ~ /.well-known {
    allow all;
  }
  location = / {
    # temporary redirect before we server our own page
    rewrite ^/$ http://blog.nodrama.io redirect;
  }
}

运行以下命令时:

sudo certbot --nginx -d nodrama.io -d www.nodrama.io

出现以下错误:

Starting new HTTPS connection (1): acme-v01.api.letsencrypt.org
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for nodrama.io
http-01 challenge for www.nodrama.io
Waiting for verification...
Cleaning up challenges
IMPORTANT NOTES:
- The following errors were reported by the server:
  Domain: www.nodrama.io
  Type:   unauthorized
  Detail: Invalid response from
  http://www.nodrama.io/.well-known/acme-challenge/lMEKfsPUnxowmND04ky9sXbFxUERwuna9hsMRHtBv8A:
  "<html>
  <head><title>404 Not Found</title></head>
  <body bgcolor="white">
  <center><h1>404 Not Found</h1></center>
  <hr><center>"
  Domain: nodrama.io
  Type:   unauthorized
  Detail: Invalid response from
  http://nodrama.io/.well-known/acme-challenge/Wml_6nPNrPebbONMLtNbg7yUaDihATrzYluA91Era9s:
  "<html>
  <head><title>404 Not Found</title></head>
  <body bgcolor="white">
  <center><h1>404 Not Found</h1></center>
  <hr><center>"
  To fix these errors, please make sure that your domain name was
  entered correctly and the DNS A/AAAA record(s) for that domain
  contain(s) the right IP address.

用户还提供了一个测试文件的链接,可以访问:
http://nodrama.io/.well-known/acme-challenge/test.txt
http://www.nodrama.io/.well-known/acme-challenge/test.txt

解决方案

请注意以下操作注意版本差异及修改前做好备份。

方案1

根据错误信息,证书颁发机构无法在以下地址找到验证文件:
– http://www.nodrama.io/.well-known/acme-challenge/lMEKfsPUnxowmND04ky9sXbFxUERwuna9hsMRHtBv8A
– http://nodrama.io/.well-known/acme-challenge/Wml_6nPNrPebbONMLtNbg7yUaDihATrzYluA91Era9s

这是因为nginx配置中的location ~ /.well-known指令允许访问.well-known目录下的所有文件,但并没有指定如何处理这些文件。因此,当证书颁发机构尝试访问验证文件时,nginx返回了404 Not Found错误。

要解决这个问题,我们需要在nginx配置中添加一个新的location块,用于处理.well-known目录下的文件。以下是解决方案的步骤:
1. 打开nginx配置文件。
2. 在server块中添加以下代码:

location ^~ /.well-known/acme-challenge/ {
    default_type "text/plain";
    root /usr/share/nginx/html;
}
  1. 保存并关闭文件。
  2. 重新加载nginx配置:
sudo service nginx reload

这样,当证书颁发机构尝试访问验证文件时,nginx将正确地返回文件内容,而不是404 Not Found错误。

方案2

如果方案1无法解决问题,可能是由于DNS配置不正确导致的。请确保以下DNS记录正确配置:

nodrama.io / A / 18.217.237.232
nodrama.io / AAAA /  2600:1f16:14a:7b00:e9ba:752c:feb8:49d5
www.nodrama.io / CNAME / nodrama.io

如果DNS记录正确配置,但问题仍然存在,请检查服务器防火墙设置,确保允许外部访问.well-known/acme-challenge目录。

方案3

如果以上两种方案都无法解决问题,可能是由于其他原因导致的。请检查以下可能的原因:
– 服务器是否有其他配置文件覆盖了.well-known/acme-challenge目录的访问权限。
– 证书颁发机构是否有其他要求或限制。
– 服务器是否有其他安全设置或限制,如SELinux或AppArmor。

如果问题仍然存在,请尝试联系证书颁发机构的支持团队,以获取进一步的帮助和指导。

请注意,以上解决方案仅供参考。根据实际情况,可能需要进行适当的调整和修改。

正文完