首页 > 解决方案 > 仅当我使用 Nginx 和通配符添加端口号时,子域才有效

问题描述

我正在使用 nginx 创建一个 Web 服务器,我确实设法让一切正常工作,但我的服务器被摧毁了,所以我需要重新做一遍,现在我面临一个我没有遇到的问题。

我使用 Nodejs 并表达

我确实安装了通配符。

当我导航到主页时,一切都很好,它正在工作,但是当我转到任何其他 pagr 时,我必须添加 :3000 才能工作,否则我会被带到主页(如果我尝试子域) 如果我不得不说“example.com/page1”,就会得到 404。

假设我的域是 example.com

要访问 dom.example.com,我需要输入 dom.example.com:3000 否则我将被带到 example.com

这是我的配置文件:

server {
    listen 80;
    server_name www.example.com example.com *.example.com;
    location / {
        proxy_pass http://localhost:3000/;
    }
}

和我的节点服务器:

const express = require('express');
const app = express();
var sub = require('wildcard-subdomains')

app.use(sub({
    namespace: 's',
    whitelist: ['www', 'app'],
}))
.get('/', (req, res) => {
    res.send("Hello World!");
})
.get('/s/:p/', (req, res) => {
    res.send(`sub domain`)
    console.log('ooo')
})

const port = 3000;
const server = app.listen(port, () => {
    console.log(`Listening on http://localhost:${port}`);
});

标签: node.jsnginxserverwildcardwildcard-subdomain

解决方案


推荐阅读