ssl - letsencrypt 和 nginx 404 错误

标签 ssl nginx digital-ocean lets-encrypt

我正在尝试使用 nginx 为我的 digital ocean 服务器配置 ssl,但我遇到了问题。 我在同一台服务器上有 4 个网站,其中 3 个运行良好,但最后一个似乎反复无常。 要设置我的证书(使用 ssl 启用 https),我正在按照本教程(使用 letsencrypt)进行操作: https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-16-04

我明确表示我对 4 个网站使用相同的证书。 运行此命令时出现的问题

sudo letsencrypt certonly -a webroot --webroot-path=/projects/mysite/staging/backend -d staging.backend.mysite.com -d www.staging.backend.mysite.com

它返回这个错误:

Failed authorization procedure. staging.backend.mysite.com (http-01): urn:acme:error:unauthorized :: The client lacks sufficient authorization :: Invalid response from http://staging.backend.mysite.com/.well-known/acme-challenge/jyYxmJYByVQS_HPf7at04LZkirwKe3rOHCeMYcNk1XA: "<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>", www.staging.backend.mysite.com (http-01): urn:acme:error:unauthorized :: The client lacks sufficient authorization :: Invalid response from http://www.staging.backend.mysite.com/.well-known/acme-challenge/-CPeTAThAt2XBMP28LiJmaJxhWDgtU6ysRvgfVv3o5s: "<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>"

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

   Domain: staging.backend.mysite.com
   Type:   unauthorized
   Detail: Invalid response from http://staging.backend.mysite.com
   /.well-known/acme-
   challenge/jyYxmJYByVQS_HPf7at04LZkirwKe3rOHCeMYcNk1XA: "<html>
   <head><title>404 Not Found</title></head>
   <body bgcolor="white">
   <center><h1>404 Not Found</h1></center>
   <hr><center>"

   Domain: www.staging.backend.mysite.com
   Type:   unauthorized
   Detail: Invalid response from
   http://www.staging.backend.mysite.com/.well-known/acme-
   challenge/-CPeTAThAt2XBMP28LiJmaJxhWDgtU6ysRvgfVv3o5s: "<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 record(s) for that domain
   contain(s) the right IP address.

当我尝试到达 http://staging.backend.mysite.com/ 时使用 Chrome,它会返回此错误:

NET::ERR_CERT_COMMON_NAME_INVALID

这是我的 nginx 配置:

server {
    listen 80;
    listen [::]:80;
    server_name staging.backend.mysite.com www.staging.backend.mysite.com;

    location / {
        proxy_pass http://127.0.0.1:8900/;
    }
}


server {
    listen 8900;
    server_name my.site.ip.adresse;

    location = /favicon.ico {
        access_log off;
        log_not_found off;
    }

    location /static/ {
        root /projects/mysite/staging/backend;
    }

    location /media/ {
        root /projects/mysite/staging/backend;
    }

    location ~ /.well-known {
        allow all;
    }

    location / {
        proxy_pass http://unix:/projects/mysite/staging/backend/run/mysite.sock;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto https;
        proxy_set_header Host $http_host;
        proxy_redirect off;
    }
}

如您所见,服务器 block 上没有 ssl 配置,因为教程说稍后添加它。 它适用于其他 3 个站点。我的配置有问题吗?

最佳答案

我通过禁用 nginx 中的代理并确保 .well-known 在我的 http 根目录中来解决这个问题(你的 8900 服务器 block 中没有根指令,添加一个作为临时指令。这必须指向文件系统中的一个目录,这将是/projects/mysite/staging/backend/.well-known 基于你给 letsencrypt 命令的 webroot 路径)。

确保您可以从浏览器访问 sample.com/.well-known,运行 Let's Encrypt 安装,然后重新打开您的代理。

您的 .well-known block 应该不是必需的。也不确定为什么你在 nginx 中同时在 80 和 8900 上服务。为什么不直接将 80 代理到您的套接字并跳过在 8900 上的服务?

关于ssl - letsencrypt 和 nginx 404 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43636914/

相关文章:

java - HttpClient ssl 和 login.facebook.com

cakephp - 在 CakePHP 项目中设置 React

nginx - 使用 NGINX-RTMP 从远程计算机播放视频

Nginx 大流量负载均衡

node.js - 无法在不阻塞 DigitalOcean 的情况下运行多个 Node 子进程

node.js - nodejs tls session ID

.net - 启用 "Require TLS session resumption on data connection when using PROT P"的 FileZilla 服务器上基于 SSL/TLS 的 FtpWebRequest

java - Spring security - 启用 HTTPS - 客户端收到 403 未经授权

linux - digital ocean 一键式应用

Kubernetes简单认证