nginx vhost 子域坏网关

问题描述

我正在尝试在 Nginx 中设置一些 vhost 子域。我的主域似乎工作正常;它提供应有的文件。但是,子域给出了错误的网关错误。 理想情况下,我想做的是使用 http 来处理任何 GET 请求,但将 https 用于我的套接字连接,这将是子域应用程序的基础。 这是我的 Nginx 服务器块:

server {
    listen 80;
    listen 443 ssl;
    server_name example.com;
    
    root /var/www/example/html
    
    ssl_certificate /var/www/example/ssl/certificate.crt;
    ssl_certificate_key /var/www/example/ssl/private.key;
    
    index index.htm index.html
    
    location / {
        try_files $uri $uri/ =404;
    }
}


server {
    listen 80;
    listen 443 ssl;
    server_name chat.example.com;
    
    ssl_certificate /var/www/example/ssl/certificate.crt;
    ssl_certificate_key /var/www/example/ssl/private.key;
    
    location / {
        proxy_pass https://localhost:8443;
        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;
    }
}

这是子域的 nodejs 后端:

require('dotenv').config();

var fs = require('fs'),http = require('http'),https = require('https');
var express = require('express'),app = express(),opts = {
        cert: fs.readFileSync("../example/ssl/certificate.crt"),key: fs.readFileSync("../example/ssl/private.key"),requestCert: true,ca: [
            fs.readFileSync('../example/ssl/cert.ca-bundle')
        ]
    };

app.get('/',function (req,res) {
    res.send('hello world')
})

var httpServer = http.createServer(app);
var httpsServer = https.createServer(opts,app);
httpServer.listen(80);
httpsServer.listen(8443);

global.express = express;

process.on('uncaughtException',(err) => {
    console.log("Uncaught Exception:",err);
    process.exit(1);
});

const io = require("socket.io")(httpsServer);

io.on("connection",socket => {
    socket.send("Connected.");

    socket.on("message",(data) => {
        console.log(data);
    });
});

console.log('Running');

我应该注意的是,SSL 证书是一个通配符证书,应该涵盖 example.com 的所有子域

我至少需要做什么才能让子域代理到节点?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)