AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 1017545
Accepted
FreeSoftwareServers
FreeSoftwareServers
Asked: 2020-05-18 14:29:29 +0800 CST2020-05-18 14:29:29 +0800 CST 2020-05-18 14:29:29 +0800 CST

无法使用 LetsEncrypt - CertBot - 设置 HTTP 到 HTTPS 重定向时

  • 772

我正在尝试配置 CertBot,它仅在我通过 http 服务我的网站时才有效。通常我有一个 https 重定向,我不想每次需要使用 certbot 时都更改站点配置。我试图只/.well-known/通过 http 服务,但它仍然没有解决这个问题的任何想法?

我正在尝试复制这个想法但没有工作-> NGINX 将所有内容重定向,除了让加密到 https

例如:这有效:

server {
    listen 80;
    listen [::]:80;
    server_name example.com www.example.com;


location / {

        proxy_pass              http://localhost:8575/;
        include                 /etc/nginx/conf.d/proxy.conf;
    }
}

这不:(请注意,当前配置的 SSL 证书不正确,但需要 NGinX 启动)

server {

   listen 80;
   listen [::]:80;
   server_name www.example.com example.com;

    location /.well-known/acme-challenge/ {
        proxy_pass              http://localhost:8575/;
        include                 /etc/nginx/conf.d/proxy.conf;
    }

location / {
       return 301 https://$server_name$request_uri;
    }

}

server {
        listen 443 ssl;
        listen        [::]:443;
        server_name www.example.com example.com;

#        ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
#        ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
        ssl_certificate /etc/ssl/crt/crt.crt;
        ssl_certificate_key /etc/ssl/crt/key.key;

location / {

        proxy_pass              http://localhost:8575/;
        include                 /etc/nginx/conf.d/proxy.conf;
    }
}

错误日志:

certbot    | Saving debug log to /var/log/letsencrypt/letsencrypt.log
certbot    | Plugins selected: Authenticator webroot, Installer None
certbot    | Registering without email!
certbot    | Obtaining a new certificate
certbot    | Performing the following challenges:
certbot    | http-01 challenge for www.example.com
certbot    | http-01 challenge for example.com
certbot    | Using the webroot path /var/www/html for all unmatched domains.
certbot    | Waiting for verification...
certbot    | Challenge failed for domain www.example.com
certbot    | Challenge failed for domain example.com
certbot    | http-01 challenge for www.example.com
certbot    | http-01 challenge for example.com
certbot    | Cleaning up challenges
certbot    | IMPORTANT NOTES:
certbot    |  - The following errors were reported by the server:
certbot    |
certbot    |    Domain: www.example.com
certbot    |    Type:   unauthorized
certbot    |    Detail: Invalid response from
certbot    |    http://www.example.com/.well-known/acme-challenge/WyVEA5g6BWVDPpYUhEJ0bG5iH6daF1rZpFd0vuTXOa0
certbot    |    [50.117.156.123]: "        <!DOCTYPE html><html lang=\"en-US\">\r\n
certbot    |    \t<head>\n\n\t\t        <meta charset=\"UTF-8\">\r\n        <meta
certbot    |    name=\"viewport\" con"
certbot    |
certbot    |    Domain: example.com
certbot    |    Type:   unauthorized
certbot    |    Detail: Invalid response from
certbot    |    https://www.example.com/x61_h9wxFY2Ye8-16GllyMq_dfsXbsEB1lYOjeq4LjU
certbot    |    [50.117.156.123]: "        <!DOCTYPE html><html lang=\"en-US\">\r\n
certbot    |    \t<head>\n\n\t\t        <meta charset=\"UTF-8\">\r\n        <meta
certbot    |    name=\"viewport\" con"
certbot    |
certbot    |    To fix these errors, please make sure that your domain name was
certbot    |    entered correctly and the DNS A/AAAA record(s) for that domain
certbot    |    contain(s) the right IP address.
certbot    |  - Your account credentials have been saved in your Certbot
certbot    |    configuration directory at /etc/letsencrypt. You should make a
certbot    |    secure backup of this folder now. This configuration directory will
certbot    |    also contain certificates and private keys obtained by Certbot so
certbot    |    making regular backups of this folder is ideal.
certbot    | Some challenges have failed.
certbot exited with code 1
nginx https certbot lets-encrypt
  • 2 2 个回答
  • 4363 Views

2 个回答

  • Voted
  1. Esa Jokinen
    2020-05-23T00:10:34+08:002020-05-23T00:10:34+08:00

    使用HTTP-01 质询,可以先重定向到 HTTPS 并通过 TLS 提供质询。但是,挑战总是从使用端口 80 的普通 HTTP 连接开始,您只能重定向到端口 443 上的 HTTPS。

    我们对 HTTP-01 挑战的实现遵循重定向,深度最多 10 个重定向。它只接受重定向到“http:”或“https:”,并且只接受到端口 80 或 443。它不接受重定向到 IP 地址。当重定向到 HTTPS URL 时,它不会验证证书(因为此质询旨在引导有效证书,它可能会在此过程中遇到自签名或过期的证书)。

    HTTP-01 质询只能在端口 80 上完成。允许客户端指定任意端口会降低质询的安全性,因此 ACME 标准不允许这样做。

    因此,这种 Nginx 配置也应该可以工作:

    server {
        listen 80;
        server_name www.example.com example.com;
    
        location / {
            return 301 https://$server_name$request_uri;
        }
    }
    
    server {
        listen 443 ssl;
        server_name www.example.com example.com;
    
        location /.well-known/acme-challenge/ {
            # HTTP-01 challenge
        }
    
        location / {
            # Your web application
        }
    }
    

    在您的情况下,这意味着以下内容可能位于 HTTP 或 HTTPSserver块中。

    location /.well-known/acme-challenge/ {
        proxy_pass   http://localhost:8575/.well-known/acme-challenge/;
        include      /etc/nginx/conf.d/proxy.conf;
    }
    

    您能够替换为/.well-known/acme-challenge/,$request_uri因为:

    在proxy_pass中使用变量时:

    location /name/ {
        proxy_pass http://127.0.0.1$request_uri;
    }
    

    在这种情况下,如果在指令中指定了 URI,它将按原样传递给服务器,替换原始请求 URI。

    此外,如果/具有相同的根,则根本不需要单独location的。

    • 4
  2. Best Answer
    FreeSoftwareServers
    2020-05-18T14:43:12+08:002020-05-18T14:43:12+08:00

    更新:我的主要问题是没有使用$request_uriw/proxy_pass指令(这也不允许~BTW)。但是,在 HTTPS 块中使用它并没有错。查看https://serverfault.com/a/1018199/312793和

    https://serverfault.com/a/1017720/312793我意识到我实际上不需要传递我的 webapp 的“真实”根目录,只需在 nginx 可以服务于 certbot 读取/写入文件的地方。同样,您可以让一个目录为多个站点提供服务,因此我决定将最熟练的方法添加到locationnginxdefault服务器块内100%。事实上,web 应用程序甚至不需要运行,只需要运行 nginx。

    这是我的新default服务器块。注意:我acme在我的 nginx“真实”webroot 中创建了一个文件夹,并为该目录提供服务location /.well-known/acme-challenge/

    server {
            listen 80 default_server;
            listen [::]:80 default_server;
            root /var/www/html/;
            index index.html;
    }
    server {
            listen 443 default_server;
            listen [::]:443 default_server;
            ssl_certificate /etc/ssl/fake/fake.crt;
            ssl_certificate_key /etc/ssl/fake/fake.key;
    
            location /.well-known/acme-challenge/ {
            root /var/www/html/acme;
             allow all;
            }
    
            root /var/www/html/;
            index index.html;
    }
    

    就像在进行设置时一样,您需要为 SSL 证书准备一些东西,否则 nginx 将无法正确启动。对此设置/分辨率非常满意!


    需要添加以下内容:$request_uri

    location /.well-known/acme-challenge/ {
        proxy_pass              http://localhost:8575/$request_uri;
        include                 /etc/nginx/conf.d/proxy.conf;
    }
    
    • 1

相关问题

  • Gzip 与反向代理缓存

  • nginx 作为代理的行为

  • Nginx 学习资源 [关闭]

  • 提供 70,000 个静态文件 (jpg) 的最佳方式?

  • 在 Apache、LightTPD 和 Nginx Web 服务器上提供 PHP 5.x 应用程序的现状?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve