Handdom Express Subdomain with nginx

I am wondering how I can handle subdomains in my project based on Expressjs.

Here is the configuration of My 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; } } 

and here is 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 displays a pointer route

and of course I configured the hosts

I was looking for 3 hours, you can help me :)

+7
subdomain nginx express
source share
1 answer

There are several requirements:

  • Setting Host header in nginx with desired domain or proxy, if applicable
  • Use middleware subdomain before other intermediaries that process 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 hard-coded host header values:

I believe that you have configured the node header incorrectly. try the following 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 application :

 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); 
+10
source share

All Articles