微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

nginx vhost 子域坏网关

如何解决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 举报,一经查实,本站将立刻删除。