如何使用节点旁边-HTTP代理虚拟主机? [英] How to use vhosts alongside node-http-proxy?

查看:255
本文介绍了如何使用节点旁边-HTTP代理虚拟主机?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我运行的NodeJS和Apache对方一起。

节点的HTTP代理正在侦听端口80,然后将请求转发要么阿帕奇(:9000)或前preSS(:8000)。

我在Apache虚拟主机是这样的:

 <虚拟主机127.0.0.1>
    的DocumentRoot/本地主机/ myVhost
    服务器名myVhost
< /虚拟主机>

我的问题是,什么是正确的方式有虚拟主机像防爆preSS /方的NodeJS功能?我想preFER不必须把自己的端口上的每个应用程序的NodeJS作为这里建议:

https://github.com/nodejitsu/node-http-proxy
(见标题为使用主机名只有ProxyTable代理请求)

我注意到连接(而据我了解,在得到前preSS捆绑)有一些虚拟主机的功能。我应该使用吗?如果是这样,这将是运行它旁边的正确途径节点的HTTP代理?

<一个href=\"http://www.senchalabs.org/connect/middleware-vhost.html\">http://www.senchalabs.org/connect/middleware-vhost.html

我也注意到了这个名为集群等模块,它似乎是相关的,但我不知道如何:

http://learnboost.github.com/cluster/

虽然不希望压倒,我还隔着一个叫过来,海捕这似乎是相关的,但我不知道如果这纯粹是一个全置换,使用虚拟主机:

https://github.com/nodejitsu/haibu

请注意:我是一个前端的家伙,所以我不是很熟悉,很多服务器术语


解决方案

我从来没有想通了海捕或群集。但我没有发现,解决了我的问题一个很好的解决方案。令我惊讶的是,它实际上是相当简单的。但是,我不知道很多有关服务器,因此这个工作,它可能不是最优的。

我设置虚拟主机正常人一样在Apache
(http://httpd.apache.org/docs/2.0/vhosts/examples.html)

我安装节点以下


  • 防爆preSS(HTTP://ex$p$pssjs.com/)

  • 节点-HTTP代理(https://github.com/nodejitsu/node-http-proxy)

那么,作为个人风格问题,我把我所有的虚拟主机在一个公共目录(/本地主机)

我然后切换Apache监听其他的端口,端口80。我只是碰巧选择端口9000,因为我已经看到的地方使用的。 (在httpd.conf中,改变听80到听9000)。我还必须确保所有我的虚拟主机,在加时赛中/的httpd-vhosts.conf定义设定为基于IP了NameVirtualHost(127.0.0.1)而不是使用一个端口。(*:80)

在节点方面,我创建了一个在端口8000听我的应用程序/服务器(又名节点虚拟主机)(有点任意选择的端口号)有关创建与前preSS服务器此链接: HTTP://ex$p$pssjs.com/guide.html

在我的/本地主机目录然后,我创建了一个名为nodeHttpProxy.js

使用节点HTTP代理,在nodeHttpProxy.js然后我创建了一个端口上侦听80前使用preSS,它包装连接代理服务器(http://www.senchalabs.org/connect/)我创造了我的虚拟主机。

该nodeHttpProxy.js文件看起来是这样的:

  //模块依赖关系
VAR HTTPPROXY =要求('的/ usr / local / lib目录/ node_modules / HTTP代理/ ​​lib目录/节点HTTP代理)
,前preSS =要求('的/ usr / local / lib目录/ node_modules / EX preSS / lib中/ EX preSS');// HTTP代理服务器
httpProxy.createServer(功能(REQ,资源,代理){    //节点的主机名称的数组
    VAR nodeVhosts = [
        VHOST1
        vhost2
    ]
    ,主机= req.header('主机')
    端口= nodeVhosts.indexOf(主机)&GT; -1
        ? 8000
        :9000;    //现在代理请求
    proxy.proxyRequest(REQ,资源,{
        主持人:主持人
        ,端口:端口
    });
})
。听(80);//虚拟主机服务器
前press.createServer()
。使用(如press.vhost('VHOST1,要求('./ VHOST1 /应用')))
。使用(如press.vhost('vhost2,要求('./ vhost2 /应用')))
.app.listen(8000);

正如你所看到的,我会在每次我创建一个新的节点虚拟主机时间做两件事情:


  1. 添加虚拟主机名到我的nodeVhosts数组

  2. 使用.SET方法定义一个新的前preSS虚拟主机

当然,我也会在我的/本地主机目录下创建实际的主机路径/文件。

一旦完成所有这些我只需要运行nodeHttpProxy.js:

 节点nodeHttpProxy.js

您可能会得到一些奇怪的EACCESS的错误,在这种情况下,只需要运行和sudo的。

这将监听80端口,如果主机相匹配的nodeVhosts数组中的名称之一,将请求转发到端口8000主机,否则会转发请求到端口9000。<主机/ p>

I'm running Nodejs and Apache alongside each other.

node-http-proxy is listening on port 80 and then forwarding requests to either Apache(:9000) or to Express(:8000).

My virtual hosts on Apache look like:

<VirtualHost 127.0.0.1>
    DocumentRoot "/localhost/myVhost"
    ServerName myVhost
</VirtualHost>

My question is, what is the "correct" way to have vhost like functionality on the Express/Nodejs side? I would prefer to not have to place each Nodejs app on its own port as is suggested here:

https://github.com/nodejitsu/node-http-proxy (Section titled "Proxy requests using a 'Hostname Only' ProxyTable")

I noticed Connect (which as I understand it, gets bundled in Express) has some vhosts functionality. Should I be using that? If so, what would be the correct way to run it alongside node-http-proxy?

http://www.senchalabs.org/connect/middleware-vhost.html

I also noticed this other module called "Cluster", it seems to be related but I'm not sure how:

http://learnboost.github.com/cluster/

While not wanting to overwhelm, I also came across one called, "Haibu" it seems to be related but I'm not sure if it would just be an all out replacement for using vhosts:

https://github.com/nodejitsu/haibu

Note: I'm a front-end guy, so I'm not very familiar with a lot of server terminology

解决方案

I never figured out Haibu or Cluster. But I did find a good solution that addressed my issue. To my surprise, it was actually quite simple. However, I don't know much about servers, so while this works, it may not be optimal.

I set up virtual hosts like normal on Apache (http://httpd.apache.org/docs/2.0/vhosts/examples.html)

I installed the following on Node

  • Express (http://expressjs.com/)
  • node-http-proxy (https://github.com/nodejitsu/node-http-proxy)

Then, as a matter of personal style, I placed all my virtual hosts in a common directory (/localhost)

I then switched Apache to listen on a port other than port 80. I just happened to choose port 9000 because I had seen that used somewhere. (In httpd.conf, changed "Listen 80" to "Listen 9000"). I also had to make sure that all my virtual hosts, as defined in extra/httpd-vhosts.conf were set to an IP based nameVirtualHost (127.0.0.1) instead of using a port (*:80).

On the Node side, I created my app/server (aka node virtual host) that listened on port 8000 (somewhat arbitrarily choice of port number) See this link on creating a server with express: http://expressjs.com/guide.html

In my /localhost directory I then created a file called "nodeHttpProxy.js"

Using node-http-proxy, in nodeHttpProxy.js I then created a proxy server that listens on port 80. Using express, which wraps connect (http://www.senchalabs.org/connect/) I created my virtual hosts.

The nodeHttpProxy.js file looks like this:

// Module dependancies
var httpProxy = require('/usr/local/lib/node_modules/http-proxy/lib/node-http-proxy')
, express = require('/usr/local/lib/node_modules/express/lib/express');

// Http proxy-server
httpProxy.createServer(function (req, res, proxy) {

    // Array of node host names
    var nodeVhosts = [
        'vhost1'
        , 'vhost2'
    ]
    , host = req.header('host')
    , port = nodeVhosts.indexOf(host) > -1
        ? 8000
        : 9000;

    // Now proxy the request
    proxy.proxyRequest(req, res, {
        host: host
        , port: port
    });
})
.listen(80);

// Vhosts server
express.createServer()
.use(express.vhost('vhost1', require('./vhost1/app')))
.use(express.vhost('vhost2', require('./vhost2/app')))
.app.listen(8000);

As you can see, I will have to do two things each time I create a new Node virtual host:

  1. add the virtual host name to my "nodeVhosts" array
  2. define a new express virtual host using the .set method

Of course, I will also have to create the actual host path/files in my /localhost directory.

Once all this is done I just need to run nodeHttpProxy.js:

node nodeHttpProxy.js

You might get some weird "EACCESS" error, in which case, just run as sudo.

It will listen on port 80, and if the host matches one of the names in the nodeVhosts array it will forward the request to that host on port 8000, otherwise it will forward the the request onto that host on port 9000.

这篇关于如何使用节点旁边-HTTP代理虚拟主机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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