我想知道我如何处理基于Expressjs的项目中的子域名.
这是我的nginx
配置
server { listen 80; server_name bee.local; access_log /var/log/nginx/bee.local.access.log; error_log /var/log/nginx/bee.local.error.log; location / { proxy_pass http://127.0.0.1:3000; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header X-Forwarded-For $remote_addr; } } server { listen 80; server_name api.bee.local; access_log /var/log/nginx/bee.local.access.log; error_log /var/log/nginx/bee.local.error.log; location / { proxy_pass http://127.0.0.1:3000; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header X-Forwarded-For $remote_addr; } }
这是我的子域支持路由器
router.get('/v1/', function(req, res, next) { res.status(200).json({ title: "title" }); }); app.use(subdomain('api', router));
问题是它正在渲染索引路由
并且肯定我设置了hosts
文件
我一直在寻找3小时你可以帮助我:)
有几个要求:
在nginx中使用必需的域或代理设置主机头(如果适用)
在处理端点的其他中间件之前使用子域中间件
工作示例:
nginx配置:
server { listen 80; server_name bee.local; location / { proxy_pass http://127.0.0.1:3000; proxy_http_version 1.1; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } } server { listen 80; server_name api.bee.local; location / { proxy_pass http://127.0.0.1:3000; proxy_http_version 1.1; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } }
具有硬编码主机头值的nginx配置:
我相信你没有正确设置主机头.请尝试下一个配置
server { listen 80; server_name bee.local; location / { proxy_pass http://127.0.0.1:3000; proxy_http_version 1.1; # proxy_set_header Host $host; proxy_set_header Host bee.local; } } server { listen 80; server_name api.bee.local; location / { proxy_pass http://127.0.0.1:3000; proxy_http_version 1.1; # proxy_set_header Host $host; proxy_set_header Host api.bee.local; } }
快递应用:
var subdomain = require('express-subdomain'); var express = require('express'); var app = express(); var router = express.Router(); router.get('/', function(req, res) { res.send('Welcome to our API!'); }); router.get('/users', function(req, res) { res.json([ { name: "Brian" } ]); }); app.use(subdomain('api', router)); app.get('/', function(req, res) { res.send('Homepage'); }); app.listen(3000);