在 nginx 服务器中安装 SSL 证书后无法登录我的节点应用程序

在 nginx 服务器中安装 SSL 证书后无法登录我的节点应用程序

我有一个节点应用程序,其前端部署在 Nginx http Web 服务器中,后端(分子微服务)部署在 AWS ec2 Linux VM 上的节点服务器中。应用程序 URL 本质上是这个 Linux 服务器主机名或公共 ipv4 地址(http://hostname:80 或 http://ipv4:8080 ),现在,由于这个 URL 不安全,我考虑使用 SSL 来保护它,为了能够做到这一点,我从 cloudns.net 购买了一个域名并将其指向我的 ec2 实例的公共 ipv4。现在,我使用 certbot 和 letsencrpt.org 在 Nginx 服务器中安装了 SSL 证书,现在我的登录页面中有 https,并且 SSL 证书显示正确(https://host.mypurchaseddomainname)。现在,当我尝试通过传递凭据登录到我的应用程序时,它不让我进入,并且在使用检查元素检查后(我使用的是 chrome),请求 URL 没有返回任何内容,错误为 ERR_CONNECTION_CLOSED。此请求 URL 是一种在 8082 端口上发出请求的 post 方法。请求 URL 如下所示:https://hostname:8082/v1/auth/signin 这里要提的另一件事是,在安装 SSL 之前,应用程序的端点 URL 可以附加端口号 80、8080,并且在输入时,它最终将变成主机名(http://hostname/signin?)但在安装 SSL 之后,如果我将任何内容附加到 https URL(如 https://mypurchasedhostname:8080),它会引发错误:无法访问该站点,这显然与登录请求 URL 发生的情况相同。尽管这些端口(80,8080)在与 http://hostname 一起提供时最终变为 https://hostname/somewelcometext 并且登录页面打开。

我不确定这是否是服务器配置问题,或者后端是否也需要更改。请注意,我的登录请求 URL 之前是 http,我从后端将其更改为 https,以避免混合内容错误。以下是nginx.conf我添加 SSL 配置之前和之后的文件。

沒有 SSL:

sudo vim /etc/nginx/nginx.conf

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

  #Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

     server {
        listen       80 default_server;
        listen       8080 default_server;
        server_name  mypurchasedhostname;
        root         /usr/share/nginx/html;
        index        index.html;



        # Load configuration files for the default server block.
        include /etc/nginx/default.d/*.conf;

        location / {
            try_files $uri $uri/ /index.html;
            add_header Cache-Control 'no-cache';
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }}

使用 SSL:

sudo vim /etc/nginx/nginx.conf

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

    # Load dynamic modules. See /usr/share/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 2048;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

     server {
        listen       80 default_server;
        listen       8080 default_server;
        server_name  mypurchasedhostname;
       # root         /usr/share/nginx/html;
       # index        index.html;
       return 301 https://$host$request_uri;

    }


#SSL settings

server {

    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name  mypurchasedhostname;
    root         /usr/share/nginx/html;
    index        index.html;

    ssl_certificate /etc/letsencrypt/live/mypurchasedhostname/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/mypurchasedhostname/privkey.pem;
    ssl_session_timeout 1d;
    ssl_session_cache shared:MozSSL:10m;  # about 40000 sessions
    ssl_session_tickets off;

    # curl https://ssl-config.mozilla.org/ffdhe2048.txt > /path/to/dhparam
    # ssl_dhparam /path/to/dhparam;

    # intermediate configuration
    ssl_protocols TLSv1.2;
    ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384;
    ssl_prefer_server_ciphers on;

    # HSTS (ngx_http_headers_module is required) (63072000 seconds)
    add_header Strict-Transport-Security "max-age=63072000" always;

    # OCSP stapling
    ssl_stapling on;
    ssl_stapling_verify on;

    # verify chain of trust of OCSP response using Root CA and Intermediate certs
    # ssl_trusted_certificate /path/to/root_CA_cert_plus_intermediates;

    # replace with the IP address of your resolver
    resolver 8.8.8.8;
    # Load configuration files for the default server block.

        include /etc/nginx/default.d/*.conf;

        location / {
            try_files $uri $uri/ /index.html;
            add_header Cache-Control 'no-cache';
        }

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }

}

}

相关内容