node.js - SSL 的 Nginx 配置 - React - Node - Cloudflare

标签 node.js reactjs ssl nginx cloudflare

我真的是 Nginx 的菜鸟,我不知道如何正确配置 nginx,我的项目文件夹是它

/Client (react)
/Server (node)
/ (server config)

我之前有这个 nginx 配置,一切都很完美

server {
    listen 80;

    server_name IP;

    location / {
        proxy_pass http://IP:3030;
        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;
    }
}

稍后在 Cloudflare 上我激活了仅 https,我的网站无法连接到端口 :4000 上的后端,经过一些研究我发现我需要更改 nginx 配置,这是 Cloudflare 指导我创建的。

server {
  listen     80;
  listen     443;

  ssl        on;
  ssl_certificate         /root/certificate.pem;
  ssl_certificate_key     /root/key.key;

  server_name    mydomain.org www.mydomain.org;
  access_log     /var/log/nginx/nginx.vhost.access.log;
  error_log      /var/log/nginx/nginx.vhost.error.log;
  location / {   # i just paste everything i had on the previous config here
    proxy_pass https://IP:3030;
    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;
  }
}

基本上,网站加载但无法处理对后端的请求。

如果你不知道为什么我有 /server 文件夹和 / 文件夹,这是我的 server.js 文件:

const express = require("express");
const path = require("path");
const app = express();

const port = process.env.PORT || 3030;
app.use(express.static(path.join(__dirname, "client/build")));
app.get("/*.png", (req, res) => {
  console.log(req.params[0]);
  res.sendFile(path.join(__dirname, "client/public/", req.params[0] + ".png"));
});
app.get("/*.jpeg", (req, res) => {
  console.log(req.params[0]);
  res.sendFile(path.join(__dirname, "client/public/", req.params[0] + ".jpeg"));
});

app.get("/*", (req, res) => {
  res.sendFile(path.join(__dirname, "client/build/", "index.html"));
});

app.listen(port, () => console.log(`Listening on port ${port}`));

抱歉发了这么长的帖子,但我对nginx一无所知,我想涵盖几乎所有我拥有的东西。

最佳答案

也许您的后端服务器没有准备好处理 SSL 请求。你可以让你的 NGINX 为你处理,并在没有 SSL 的情况下与你的后端通信,这在以前是有效的。您可以通过将 proxy_pass 指令改回 http:// 地址来实现。

server {
  # Other stuff ...
  location / {
     # Comunicate proxy with backend without encryption until you setup SSL on your application...
     proxy_pass http://IP:3030;
     # Other stuff...
  }
}

关于node.js - SSL 的 Nginx 配置 - React - Node - Cloudflare,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51834072/

相关文章:

node.js - 使用sequelize-cli后如何从sequelize.model获取模型

node.js - Nodejs如何原始html

javascript - 样式被 Material-UI 样式覆盖

SSL 验证失败 - knife ec2 with chef 12

linux - apache https 配置问题

javascript - Jasmine Node 要求 *

node.js - 错误: Received unknown parameter: business_type - Node Stripe Connect

javascript - data.map 不是一个函数 React

javascript - 在 React 的按键上应用按钮 'Active' 样式

使用 Nginx 的 SSL websockets 无法连接