使用node-http-proxy的默认路由? [英] Default route using node-http-proxy?

查看:156
本文介绍了使用node-http-proxy的默认路由?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做一个简单的node.js反向代理,以在同一端口80上托管多个Node.JS应用程序和我的apache服务器.因此,我找到了这个示例

I want to do a simple node.js reverse proxy to host multiple Node.JS applications along with my apache server on the same port 80. So I found this example here

var http = require('http')
, httpProxy = require('http-proxy');

httpProxy.createServer({
    hostnameOnly: true,
    router: {
        'www.my-domain.com': '127.0.0.1:3001',
        'www.my-other-domain.de' : '127.0.0.1:3002'
    }
}).listen(80);

问题是,例如,我想让app1.my-domain.com指向localhost:3001,app2.my-domain.com指向localhost:3002,所有其他都进入端口3000,例如我的apache服务器将运行.我在文档中找不到有关如何使用默认"路由的任何内容.

The problem is that I want to have for example app1.my-domain.com pointing to localhost:3001, app2.my-domain.com pointing to localhost:3002, and all other go to port 3000 for example, where my apache server will be running. I couldn't find anything in the documentation on how to have a "default" route.

有什么想法吗?

编辑我想这样做是因为我的apache服务器处理了很多域/子域,并且我不想每次都要添加时都不必修改此路由表一个新的子域.

EDIT I want to do that because I have a lot of domains/subdomains handled by my apache server and I don't want to have to modify this routing table each time I have want to add a new subdomain.

推荐答案

近一年来,我已经成功地使用接受的答案来拥有默认主机,但是现在,node-http-proxy允许使用一种更简单的方法主机表中的RegEx.

For nearly a year, I had successfully used the accepted answer to have a default host, but there's a much simpler way now that node-http-proxy allows for RegEx in the host table.

var httpProxy = require('http-proxy');

var options = {
  // this list is processed from top to bottom, so '.*' will go to
  // '127.0.0.1:3000' if the Host header hasn't previously matched
  router : {
    'example.com': '127.0.0.1:3001',
    'sample.com': '127.0.0.1:3002',
    '^.*\.sample\.com': '127.0.0.1:3002',
    '.*': '127.0.0.1:3000'
  }
};

// bind to port 80 on the specified IP address
httpProxy.createServer(options).listen(80, '12.23.34.45');

要求您不要将hostnameOnly设置为true,否则将不处理RegEx.

The requires that you do NOT have hostnameOnly set to true, otherwise the RegEx would not be processed.

这篇关于使用node-http-proxy的默认路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆