使用nginx处理Express子域

时间:2015-12-28 10:49:40

标签: node.js express nginx subdomain

我想知道如何处理基于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小时你能帮助我:)

1 个答案:

答案 0 :(得分:14)

有几个要求:

  1. 在nginx中设置带有必填域或代理的主机标头(如果适用)
  2. 在处理端点的其他中间件之前使用子域中间件
  3. 工作示例:

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