NGINX反向代理HTTPS尽管certbot SSL仍无法正常工作

问题描述 投票:0回答:1

我已将NGINX设置为反向代理,以从单个Ubuntu 18.04 ec2实例(NGINX和节点服务器都在同一实例上)为节点服务器提供服务。 Certbot已成功安装和配置,并且HTTP路由没有问题,但是当我尝试命中HTTPS端点时,我的客户端上收到ERR_CONNECTION_CLOSED(该客户端托管在GH-Pages上,但我认为这不重要吗?)。

我的ec2实例设置为接受端口80和443上的所有流量,我的服务器正在侦听端口3333。

当前ufw设置为非活动状态,但我尝试启用它并允许'NGINX FULL'。在这种情况下,请求仍然失败,但是我收到了连接超时错误,而不是关闭连接。

NGINX错误日志示例输出:

2020/05/13 23:17:23 [error] 13581#13581: *15 connect() failed (111: Connection refused) while connecting to upstream, client: 159.xxx.xxx.35, server: api.theoog.net, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:3333/", host: "54.xxx.xx.xxx:80"

我的NGINX服务器块如下:

server {
  listen 443 ssl;
  server_name api.example.net www.api.example.net;

  ssl_certificate /etc/letsencrypt/live/api.example.net/fullchain.pem;
  ssl_certificate_key /etc/letsencrypt/live/api.example.net/privkey.pem;

  ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  ssl_prefer_server_ciphers on;
  ssl_ciphers 'EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH';

  location / {
     proxy_pass http://localhost:3333/;
     proxy_http_version 1.1;
     proxy_set_header Upgrade $http_upgrade;
     proxy_set_header Connection "upgrade";
     proxy_set_header Host $http_host;
     proxy_set_header X-Real-IP $remote_addr;
     proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
     proxy_set_header X-Forward-Proto http;
     proxy_set_header X-Nginx-Proxy true;
     proxy_redirect off;
  }
}

server {
 listen 80;
 server_name api.example.net www.api.example.net;
 # return 301 https://$host$request_uri;
   location / {
     proxy_pass http://localhost:3333/;
     proxy_http_version 1.1;
     proxy_set_header Upgrade $http_upgrade;
     proxy_set_header Connection "upgrade";
     proxy_set_header Host $http_host;
     proxy_set_header X-Real-IP $remote_addr;
     proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
     proxy_set_header X-Forward-Proto http;
     proxy_set_header X-Nginx-Proxy true;
     proxy_redirect off;
  }

}

我过去18个小时一直在搜寻Google,而且我的大脑正在流血,所有建议都将不胜感激。

ssl amazon-ec2 ubuntu-18.04 nginx-reverse-proxy certbot
1个回答
0
投票

我能够通过https连接到您的服务器(您忘记在错误日志中编辑该域)。服务器本身似乎运行良好(出现错误Cannot GET /)。我怀疑您在gh页上的客户端会收到该错误,因为CORS不允许它与您的服务器通信。

[如果要允许nginx使用CORS,则可能会起作用(来自https://enable-cors.org/server_nginx.html的代码段)。它开始工作后,您可能应该允许不允许所有来源来提高安全性。

#
# Wide-open CORS config for nginx
#
location / {
     if ($request_method = 'OPTIONS') {
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        #
        # Custom headers and headers various browsers *should* be OK with but aren't
        #
        add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
        #
        # Tell client that this pre-flight info is valid for 20 days
        #
        add_header 'Access-Control-Max-Age' 1728000;
        add_header 'Content-Type' 'text/plain; charset=utf-8';
        add_header 'Content-Length' 0;
        return 204;
     }
     if ($request_method = 'POST') {
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
        add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
     }
     if ($request_method = 'GET') {
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
        add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
     }
}

我希望这会有所帮助!

© www.soinside.com 2019 - 2024. All rights reserved.