koa-subdomain
koa-subdomain copied to clipboard
nginx conf example
can you give me a config example of nginx?
server {
listen 80;
server_name *.example.com;
location / {
proxy_pass http://127.0.0.1:3000/;
proxy_redirect off;
}
}
can this one work perfectly?
You need to pass Host
field to nodejs, koa-subdomain will select different route based on the Host
field.
server {
listen 80;
server_name *.example.com;
proxy_set_header Host $http_host;
location / {
proxy_pass http://127.0.0.1:3000/;
proxy_redirect off;
}
}
like this?
Almost that, you can try to debug.
const Koa = require('koa');
const Subdomain = require('koa-subdomain');
const router = require('koa-route');
const app = new Koa();
const subdomain = new Subdomain();
subdomain.use('*', router.get('/', async (ctx) => {
ctx.body = ctx.subdomains;
}));
app.use(subdomain.routes());
app.listen(4123);
sub.example.com
can visit.
sub.sub.example.com
cannot visit.
sub.sub.example.com
will match *.sub.example.com
.
Currently, *
only match 1 domain offset.
最新版本 koa-route
不再需要
new Router()
另外我还有一个疑问,可能你能帮得上,能加个 QQ 微信么
我突然发现你写的是koa-route
,而我一直用的是koa-router,对于koa-router,你不new也可以,他自己会处理的:
https://github.com/alexmingoia/koa-router/blob/5.x/lib/router.js#L44
哦。。。不要在意这些细节。。。我有问题要请教。 2539929