【问题标题】:Serve a NodeJS HTTPS app, in NGINX via proxy_pass通过 proxy_pass 在 NGINX 中提供 NodeJS HTTPS 应用程序
【发布时间】:2021-02-04 05:10:57
【问题描述】:

我有一个 NGINX 服务器,当前正在运行一个 ReactJS 应用程序,指向 /build 文件夹,通过 NGINX 启动并运行 HTPPS。

除此之外,我曾经有一个 NodeJS 应用程序(网站)在端口 8888 上运行,并带有一个反向代理,默认情况下可以通过端口 80 访问它。该应用程序以前运行在 HTTP 协议中。

这个 NodeJS 网站在 HTTPS 中提供服务的需求已经上升,我在让这两个应用程序一起运行时遇到了很多麻烦。总是 502 错误或 SLL_do_handshake 错误。

这是我的实际配置。 React 应用程序块未受影响,在我开始修补 Node 之前它运行良好。

server {
    listen 80 default_server;
    server_name _;
    return 301 https://$host$request_uri;
}

# PLATFORM  - DEFAULT SERVER
server {
    server_name platform.domain.com;
    
    listen 80 default_server;

    ssl_certificate     /etc/ssl/certs/server.crt;
    ssl_certificate_key /etc/ssl/certs/server.key;

    location / {
        proxy_pass https://xx.xx.xx.xx:8888/;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
    }

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

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

# REACT APP
server {
    server_name dashboard.domain.com;
    
    listen 443 ssl;
    root /var/www/react-app/build;
    index index.html;

    include snippets/cert-params.conf;
    include snippets/ssl-params.conf;        

    location / {
      try_files $uri $uri/ /index.html;
    }
}

我的目标是保持其当前功能:2 个独立的应用程序,在同一台机器/网络服务器上,我可以根据用户的需要相互重定向。

我错过了什么?大多数答案让我让 NGINX 处理 HTTPS 部分并让 NodeJS 应用程序在 HTTP 上运行,但我确实需要让 NodeJS 在 HTTPS 上运行。

提前致谢!

【问题讨论】:

    标签: node.js nginx nginx-reverse-proxy nginx-config


    【解决方案1】:

    由于您想将两者都作为 https 处理,因此您可以按如下方式组合块。 在 /api 上,您可以接受所有与节点相关的请求。你可以保留 / 作为你的反应路径。

    server {
        listen 80 default_server;
        server_name _;
        return 301 https://$host$request_uri;
    }
    
    # Combine React and Node in same 443 block
    server {
        server_name dashboard.domain.com;
        
        listen 443 ssl;
        
        # REACT APP
        root /var/www/react-app/build;
        index index.html;
    
        include snippets/cert-params.conf;
        include snippets/ssl-params.conf;        
    
        
        location /api {
            proxy_pass https://xx.xx.xx.xx:8888/;
            proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection 'upgrade';
            proxy_set_header Host $host;
            proxy_cache_bypass $http_upgrade;
        }
        
        location / {
          try_files $uri $uri/ /index.html;
        }
        
    }
    

    如果您首先使用 nginx 处理所有 https 请求,然后以简单的 http 协议将代理传递给节点服务器,实际上会更好,因为它可以减少节点 js 端的 ssl 处理时间。

    【讨论】:

    • 问题是,Node 应用程序是一个实际的应用程序,一个网站(我可能应该编辑我的问题)。这还能用吗?
    • 顺便说一句,我坚持在 NodeJS 端运行 HTTPS 仅仅是因为它需要向其他运行 HTTPS 的 API 发出请求,而在 HTTPS 域中请求某些内容的 HTTP 应用程序似乎不需要工作,理所当然……NGINX 处理 HTTPS,将使运行 HTTP 的应用程序能够向 HTTPS 域请求某些内容?
    • 从 node.js 后端你可以向 HTTPS 发出请求,但是如果你从浏览器发起请求,那么你需要 HTTPS。它也适用于网站。而不是 /api,您只需要提及任何名称,例如 web,以便您可以访问您的网站域/web。
    • 原来问题出在 NodeJS 方面,而我的 nginx 配置几乎都是正确的。不过,您的回答仍然很有帮助,因此非常感谢您抽出宝贵的时间。干杯伙伴。
    猜你喜欢
    • 2018-04-24
    • 2021-01-13
    • 2019-04-07
    • 2018-11-08
    • 2011-10-30
    • 2020-10-18
    • 2019-01-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多