让我们加密自动续订时出错(Nginx)

时间:2018-03-07 15:17:15

标签: javascript node.js ssl nginx greenlock

我正在尝试将greenlock-express设置为在nginx代理后面运行。

这是我的nginx配置

...
# redirect
server {
    listen 80;
    listen [::]:80;
    server_name mydomain.com;

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

# serve
server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name mydomain.com;

    # SSL settings
    ssl on;
    ssl_certificate C:/path/to/mydomain.com/fullchain.pem;
    ssl_certificate_key C:/path/to/mydomain.com/privkey.pem;

    # enable session resumption to improve https performance
    ssl_session_cache shared:SSL:50m;
    ssl_session_timeout 1d;
    ssl_session_tickets off;

    # enables server-side protection from BEAST attacks
    ssl_prefer_server_ciphers on;
    # disable SSLv3(enabled by default since nginx 0.8.19) since it's less secure then TLS
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;

    # ciphers chosen for forward secrecy and compatibility
    ssl_ciphers 'ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA:ECDHE-ECDSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA:ECDHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA256:DHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES256-SHA:ECDHE-ECDSA-DES-CBC3-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:DES-CBC3-SHA:!DSS';

    # enable OCSP stapling (mechanism by which a site can convey certificate revocation information to visitors in a privacy-preserving, scalable manner)
    resolver 8.8.8.8 8.8.4.4;
    ssl_stapling on;
    ssl_stapling_verify on;
    ssl_trusted_certificate C:/path/to/mydomain.com/chain.pem;

    # config to enable HSTS(HTTP Strict Transport Security) https://developer.mozilla.org/en-US/docs/Security/HTTP_Strict_Transport_Security
    add_header Strict-Transport-Security "max-age=31536000; includeSubdomains; preload";

    # added to make handshake take less resources
    keepalive_timeout 70;

    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-NginX-Proxy true;
        proxy_pass https://127.0.0.1:3001/;
        proxy_redirect off;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
}
...

我在端口3000(http)和端口3001(https)上运行节点服务器。其他一切似乎都有效,但证书不会更新,并在3个月后过期。

如果我关闭了nginx并在端口80(http)和端口443(https)上运行了节点服务器,那么它会更新证书。

我确保将.well-known/acme-challenge转发到节点服务器,即当我转到网址http(s)://mydomain.com/.well-known/acme-challenge/randomstr时,我得到以下回复:

{ 
  "error": { 
    "message": "Error: These aren't the tokens you're looking for. Move along." 
  } 
}

2 个答案:

答案 0 :(得分:3)

分离webroot进行ACME身份验证的简便方法。

为ACME身份验证创建一个webroot目录。

Error on request:
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 270, in run_wsgi
    execute(self.server.app)
  File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 261, in execute
    write(data)
  File "/usr/local/lib/python2.7/dist-packages/werkzeug/serving.py", line 227, in write
    self.send_header(key, value)
  File "/usr/lib/python2.7/BaseHTTPServer.py", line 412, in send_header
    self.wfile.write("%s: %s\r\n" % (keyword, value))
IOError: [Errno 32] Broken pipe

在nginx配置中,将用于ACME身份验证的webroot设置为先前创建的目录。

  

http://example.com/.well-known/acme-challenge/token - > C:/www/letsencrypt/.well-known/acme-challenge/token

C:\www\letsencrypt\.well-known

重新启动nginx。

您可以在certbot中更改您的webroot以再次进行身份验证。

server {
    listen 80;
    listen [::]:80;
    server_name mydomain.com;

    location ^~ /.well-known/acme-challenge/ {
        default_type "text/plain";
        root C:/www/letsencrypt;
    }

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

首先,通过添加certbot certonly --webroot -w C:\www\letsencrypt\ -d exapmle.com --dry-run 选项对其进行测试。否则,您可能会遇到限制身份验证尝试次数的问题。

答案 1 :(得分:1)

您看到的错误是在您的

中放置了一个令牌

根目录/。好知/ ACME挑战/令牌

然后让我们的加密尝试从互联网验证。转到http://yourdomain/.well-known/acme-challenge/token它会收到404错误 - 找不到页面。究竟为什么它得到了404我无法确定。如果您自己放置文件,是否可以从互联网访问?

如果您想知道有几种自动更新SSL的方法,而无需重新启动nginx。 nginx用户似乎更喜欢的是webroot插件:首先,使用以下内容获取新证书:

certbot certonly --webroot -w /path/to/your/webroot -d example.com --post-hook="service nginx reload"

然后设置一个cron作业,每天运行certbot次更新一次或两次;它只会在实际更新证书时运行post-hook。如果您希望停止--pre-hook以独立模式运行nginx,也可以使用certbot标记。

还有一个完整的nginx插件,您可以使用--nginx激活它。它仍在测试中,因此请自行承担风险并报告任何错误。

注意: post-hook标志将负责重新加载证书的nginx上传续订