简体   繁体   English

通过 proxy_pass 在 NGINX 中提供 NodeJS HTTPS 应用程序

[英]Serve a NodeJS HTTPS app, in NGINX via proxy_pass

I have an NGINX server currently running a ReactJS app, pointed at the /build folder, with HTPPS up and running via NGINX.我有一个 NGINX 服务器,当前正在运行一个 ReactJS 应用程序,指向 /build 文件夹,HTPPS 通过 NGINX 启动并运行。

Alongside it, I used to have a NodeJS app (website) running on port 8888 with a reverse proxy to make it accessible through port 80 by default.除此之外,我曾经有一个 NodeJS 应用程序(网站)在端口 8888 上运行,并带有反向代理,默认情况下可以通过端口 80 访问它。 This app used to run in HTTP protocol.这个应用程序曾经在 HTTP 协议中运行。

The need for this NodeJS website to be served in HTTPS has risen and I'm having quite a lot of trouble getting the two apps to function together.这个 NodeJS 网站在 HTTPS 中提供服务的需求已经上升,我在让这两个应用程序一起运行时遇到了很多麻烦。 It's always either 502 errors or SLL_do_handshake errors.它总是 502 错误或 SLL_do_handshake 错误。

This is my actual config.这是我的实际配置。 The React app block is untouched, it worked perfectly before I started tinkering with the Node one. React 应用程序块未受影响,在我开始修补 Node one 之前它运行良好。

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;
    }
}

My goal is to maintain its current function: 2 separate apps, on the same machine/webserver, that I can redirect to each other as the user requires.我的目标是保持其当前功能:在同一台机器/网络服务器上的 2 个单独的应用程序,我可以根据用户的需要相互重定向。

What am I missing?我错过了什么? Most of the answers lead me to make NGINX handle the HTTPS part and leave the NodeJS app running on HTTP, but I really need to have the NodeJS running on HTTPS.大多数答案让我让 NGINX 处理 HTTPS 部分并让 NodeJS 应用程序在 HTTP 上运行,但我确实需要让 NodeJS 在 HTTPS 上运行。

Thanks in advance!提前致谢!

As you want to handle both as https then you can combine block as below.如果您想将两者都作为 https 处理,那么您可以组合如下块。 on /api you can accept all node related requests.在 /api 上,您可以接受所有与节点相关的请求。 and you can keep / as your react path.你可以保留 / 作为你的反应路径。

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;
    }
    
}

It's actually better if you handle all https request using nginx first and then pass proxy to node server in simple http protocol as it's reduce ssl processing time on your node js side.如果您首先使用 nginx 处理所有 https 请求,然后以简单的 http 协议将代理传递给节点服务器,实际上会更好,因为它减少了节点 js 端的 ssl 处理时间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM