Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handle Express Subdomain with nginx

I Wonder how can i handle subdomains in my project that based on Expressjs.

Here's My nginx configuration

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;
  }
}

and here's my router with subdomain support

router.get('/v1/', function(req, res, next) {
    res.status(200).json({ title: "title" });
});



app.use(subdomain('api', router));

The problem is that it's rendering the index route

and for sure i setuped the hosts file

I've been searching for 3 hrs can you help me :)

like image 416
dyaa Avatar asked Dec 28 '15 10:12

dyaa


People also ask

Can I use Express with Nginx?

Nginx is super fast and powerful, making it one of the go-to choices when deploying web servers. In this blog article it is shown how an Express application can be deployed with Nginx as a reverse proxy.


1 Answers

There are several requirements:

  1. Setup Host header in nginx with required domain or proxy if applicable
  2. Use subdomain middleware before other middlewares that handle endpoints

Work example:

nginx configuration:

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 configuration with hardcoded Host header values:

I believe that you did not setup Host header correctly. please try next configuration

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;
    }
}

express app:

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);
like image 112
valery.barysok Avatar answered Oct 01 '22 05:10

valery.barysok