Nginx,Https,反向代理,握手错误。需要查看“ nginx /启用了站点/默认”文件。

时间:2018-07-16 21:08:34

标签: nginx digital-ocean nginx-reverse-proxy certbot

我希望查看下面的一些代码-它是“ nginx /启用了站点/默认”文件。我认为它可能存在一些明显的陷阱,导致我的网站无法在https下重定向。我花了几天的时间查看Nginx文档,但无法解决我的问题。感谢您的帮助!

上下文: 我正在尝试建立一个反向代理,该代理将域URL指向Digital Ocean服务器上的localhost:3000。除了我的https无法解析外,其他一切似乎都正常运行。我通常遵循以下两个教程:https://code.lengstorf.com/deploy-nodejs-ssl-digitalocean/https://www.digitalocean.com/community/tutorials/how-to-secure-nginx-with-let-s-encrypt-on-ubuntu-18-04

当我转到自己的网址时,我得到:“ ERR_CONNECTION_RESET”。当我在服务器上为http://localhost:3000运行curl时,按预期返回了我的html。当我在服务器上为https://mysite.io运行curl时,收到错误消息:“ curl(35)gnutls_handshake()失败:pull函数出错”。我所有的http curl请求都正确地重定向到https,dig + short mysite.io指向我的服务器,nginx -t正常返回。

我的直觉是问题出在我的'nginx / sites-enabled / default'文件,更具体地说是正在处理https的服务器块。前两个服务器块来自第一个教程,后两个由Certbot在上述第二个教程中自动生成。再次感谢您的帮助!

# HTTP — redirect all traffic to HTTPS
server {
    listen 80;
    listen [::]:80 default_server ipv6only=on;
    return 301 https://$host$request_uri;
}

# HTTPS — proxy all requests to the Node app
server {
    # Enable HTTP/2
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name mysite.io;

    # Use the Let’s Encrypt certificates
    ssl_certificate /etc/letsencrypt/live/mysite.io/fullchain.pem; # managed $
    ssl_certificate_key /etc/letsencrypt/live/mysite.io/privkey.pem; # manage$

    # Include the SSL configuration from cipherli.st
    include snippets/ssl-params.conf;

    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-NginX-Proxy true;
        proxy_pass http://localhost:3000/;
        proxy_ssl_session_reuse off;
        proxy_set_header Host $http_host;
        proxy_cache_bypass $http_upgrade;
        proxy_redirect off;
    }


}

server {
    return 301 https://$host$request_uri;

    server_name www.mysite.io; # managed by Certbot

    listen [::]:443 ssl ipv6only=on; # managed by Certbot
    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/mysite.io/fullchain.pem; # managed $
    ssl_certificate_key /etc/letsencrypt/live/mysite.io/privkey.pem; # manage$
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}

server {
    if ($host = www.mysite.io) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    listen 80;
    listen [::]:80  ;
    server_name www.mysite.io;
    return 404; # managed by Certbot

}

1 个答案:

答案 0 :(得分:1)

前两个块是不必要的,可以删除。然后,我们可以从第二个服务器块中获取位置块,并将其添加到第三个服务器块中。然后,最后我们可以从第三个块中删除301,最后是这样:

display: table-row-group;