Let's Encrypt 未授权 403 禁止访问

8

在服务器上,安装了Nginx。Let's Encrypt与www.domain.com正常工作,但与static.domain.com不能正常工作。

使用PuTTY时,当我输入以下命令:sudo letsencrypt certonly -a webroot --webroot-path=/var/www/site/domain -d static.domain.com -d domain.com -d www.domain.com

我遇到了以下问题:

Failed authorization procedure. static.domain.com (http-01): urn:acme:error:unauthorized :: The client lacks sufficient authorization :: Invalid response from http://static.domain.com/.well-known/acme-challenge/c6zngeBwPq42KLXT2ovW-bVPOQ0OHuJ7Fw_FbfL8XfY: "<html>
<head><title>403 Forbidden</title></head>
<body bgcolor="white">
<center><h1>403 Forbidden</h1></center>
<hr><center>"

IMPORTANT NOTES:
 - The following errors were reported by the server:

   Domain: static.domain.com
   Type:   unauthorized
   Detail: Invalid response from
   http://static.domain.com/.well-known/acme-challenge/c6zngeBwPq42KLXT2ovW-bVPOQ0OHuJ7Fw_FbfL8XfY:
   "<html>
   <head><title>403 Forbidden</title></head>
   <body bgcolor="white">
   <center><h1>403 Forbidden</h1></center>
   <hr><center>"

   To fix these errors, please make sure that your domain name was
   entered correctly and the DNS A record(s) for that domain
   contain(s) the right IP address.

有人知道问题可能是什么吗?

遇到了很多问题,被引导到各个地方,直到我尝试使用snap安装certbot。删除所有其他安装并安装它,它非常容易使用。https://certbot.eff.org/instruction - Mdev
8个回答

10

当我第一次尝试在我的网站上安装证书时,我从certbot得到了完全相同的错误消息。

检查Web服务器上的原因

我使用的是apache2而不是nginx。我查看了/var/log/apache2/error.log中与我的网站上的403 Forbidden事件相关的apache2错误消息,并发现:

[Sun Aug 26 14:16:24.239964 2018] [core:error] [pid 12345] (13)Permission denied: [client 12.34.56.78:1234] AH00035: access to /.well-known/acme-challenge/5PShRrf3tR3wmaDw1LOKXhDOt9QwyX3EVZ13JklRJHs denied (filesystem path '/var/lib/letsencrypt/http_challenges') because search permissions are missing on a component of the path

权限和访问问题

我在谷歌上搜索了这个错误信息,发现apache2无法读取上述目录(例如/var/lib/letsencrypt/http_challenges),因为存在不正确的权限设置,例如:

$ sudo ls -la /var/lib/letsencrypt/
total 16
drwxr-x---  4 root root 4096 Aug 26 14:31 .
drwxr-xr-x 72 root root 4096 Aug 18 00:48 ..
drwxr-x--- 27 root root 4096 Aug 26 14:26 backups
drwxr-xr-x  2 root root 4096 Aug 26 14:27 http_challenges

所以,根据上面这行有一个点(.)表示具有rwxr-x---权限的letsencrypt文件夹,除了根用户外,没有人可以读取其内容。为了更正权限,我只需要执行:

解决方案

$ sudo chmod o+rx /var/lib/letsencrypt

这将把上述$ ls命令的输出更改为:

$ ls -la /var/lib/letsencrypt/
total 16
drwxr-xr-x  4 root root 4096 Aug 26 14:31 .
drwxr-xr-x 72 root root 4096 Aug 18 00:48 ..
drwxr-x--- 27 root root 4096 Aug 26 14:26 backups
drwxr-xr-x  2 root root 4096 Aug 26 14:27 http_challenges

现在,上面的一行中,点(.)代表letsencrypt目录,表示rwxr-xr-x,因此“其他用户”(例如apache2的用户www-data)现在可以读取并访问letsencrypt目录。然后certbot按预期工作。

3
我必须删除我的域的AAAA记录,因为certbot更喜欢IPV6。我的Web主机提供商DNS对www和@(域的根)有默认的AAAA记录。
仔细检查/var/log/letsencrypt/letsencrypt.log - 在“addressUsed”下面,我发现它正在使用一个IPV6地址。在我的情况下,我没有任何网站在www或我的域根目录下使用IPV6地址,所以我删除了AAAA记录,并立即解决了我的问题。由于DNS传播和记录ttl,其他人可能需要更长时间才能看到缓解。
如果certbot能够解析出IPV6地址,它将尝试使用IPV6地址与您连接,即使您期望通过IPV4连接,这就是我的问题所在。
我建议在继续使用命令之前删除日志,以便只有新条目-sudo rm /var/log/letsencrypt/legsencrypt.log-在“addressUsed”中找到并验证它是否是IPV4地址而不是IPV6地址。如果它是IPV6地址,请在网关上转发该地址到您的主机并验证您也正在使用IPV6,或者从DNS中删除AAAA记录,以便letsencrypt将使用IPV4地址连接您。

谢谢。在我找到你的信息之前,这是两天的痛苦。 - Pedro Madureira

3
在您的服务器块中添加以下内容:
# for LetsEncrypt
location ~ /.well-known {
  allow all;
}

我稍微改了一下,但为什么这个不再起作用了呢? - esco_

2
我发现了一个解决方法,虽然不是自动的解决方案,但它有效。您可以使用Certbot通过DNS挑战来证明您拥有该域名。
sudo certbot -d domain.com --manual --preferred-challenges dns certonly

1

我猜你的子域名有另一个网站根目录,如果是这样,只需要指定该网站根目录即可。在你的例子中,static.domain.comdomain.com都使用相同的网站根目录。

来自https://certbot.eff.org/docs/using.html

如果你一次要为多个域名获取证书,则插件需要知道每个域名文件的服务位置,这可能是每个域名的单独目录。请求多个域名的证书时,每个域名将使用最近指定的--webroot-path参数。

certbot certonly --webroot -w /var/www/example/ -d www.example.com -d example.com -w /var/www/other -d other.example.net -d another.other.example.net


非常感谢Chris!我按照你的指示执行了以下命令: sudo certbot certonly --webroot -w /var/www/site/domain/ -d domain.com -d www.domain.com -w /var/www/site/domain/wp-content/uploads/ -d static.domain.com 但是我仍然遇到了相同的问题,即403 Forbidden。 - Dealeo - Jerome Mansbendel
我看到很多带有类似错误的线程,看起来有许多可能问题。域名网站根目录下有 .htaccess 文件吗? - Chris
该域名的Web根目录中不存在.htaccess文件。 - Dealeo - Jerome Mansbendel
1
我遇到了同样的问题,查看了这个链接.well-known ACME challenge files blocked 403 Forbidden in some Nginx configurations,它建议问题出现在nginx.conf文件中。我尝试编辑它,但是它是只读的。你有解决办法吗? - Paulo Henrique
@PauloHenrique 不好意思,我对Apache的经验更丰富,无法进行调查。 - Chris
1
@PauloHenrique 你可能不再需要它了,但感谢你的链接,我找到了一个对我有用的答案。我在这里添加了它作为一个答案。 - esco_

0

如果还有人遇到这个问题,可以尝试以下方法,对我来说有效:

location ^~ /.well-known/acme-challenge/ {
default_type "text/plain";
alias /home/nginx/domains/domain.com/public/acme-challenge/;
}

0

我不想不必要地重复,但似乎有些不同的情况可能会导致证书更新时出现403错误。对我来说,这与Wordpress/ URL重写相关的更改nginx配置有关。顺便说一下,我使用的是Virtualmin

评论中上面有一个指向Github问题的链接。 一个人精彩地解释了nginx中位置匹配是如何工作的,并提供了解决403错误的方法。尽管如此,其他问题也可能会导致这种错误。

因此,对我来说,解决方案是包括/.well-known/的位置匹配。

location ^~ /.well-known/ {
    #limit_req            [tighter per-ip settings here]; ## kicked this one out
    access_log           off;
    log_not_found        off;
    #root                 /var/www/html; ## kicked this one out
    autoindex            off;
    index                index.html; # "no-such-file.txt",if expected protos don't need it
    try_files            $uri $uri/ =404;
}

我并不是nginx专家,所以我建议您阅读这篇文章,并检查哪些参数适用于您的情况。


0
在我的情况下,我通过以下方式拒绝了对安全相关文件(/.htaccess/.htpasswd等)的访问:
location ~ /\. {
  deny all;
}

我改成了

location ~ /\.ht {
  deny all;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接