koa-subdomain icon indicating copy to clipboard operation
koa-subdomain copied to clipboard

nginx conf example

Open willin opened this issue 7 years ago • 8 comments

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?

willin avatar Mar 08 '17 03:03 willin

You need to pass Host field to nodejs, koa-subdomain will select different route based on the Host field.

keenwon avatar Mar 08 '17 03:03 keenwon

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?

willin avatar Mar 08 '17 03:03 willin

Almost that, you can try to debug.

keenwon avatar Mar 08 '17 04:03 keenwon

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.

willin avatar Mar 08 '17 06:03 willin

sub.sub.example.com will match *.sub.example.com. Currently, * only match 1 domain offset.

keenwon avatar Mar 08 '17 06:03 keenwon

最新版本 koa-route

不再需要

new Router()

另外我还有一个疑问,可能你能帮得上,能加个 QQ 微信么

willin avatar Mar 08 '17 08:03 willin

我突然发现你写的是koa-route,而我一直用的是koa-router,对于koa-router,你不new也可以,他自己会处理的:
https://github.com/alexmingoia/koa-router/blob/5.x/lib/router.js#L44

keenwon avatar Mar 08 '17 08:03 keenwon

哦。。。不要在意这些细节。。。我有问题要请教。 2539929

willin avatar Mar 08 '17 09:03 willin