如何在node-http-proxy旁边使用vhost? [英] How to use vhosts alongside node-http-proxy?

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

问题描述

我正在运行Nodejs和Apache。



node-http-proxy正在侦听端口80,然后将请求转发到Apache(:9000)或者Express(:8000)。



我的Apache上的虚拟主机如下所示:

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

我的问题是,Express / Nodejs上具有像vhost一样功能的正确方式是什么侧?我更愿意不要将每个Nodejs应用程序放在自己的端口上,如这里所建议的:



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



我注意到Connect(根据我的理解,在Express中绑定)具有一些vhosts功能。我应该用吗如果是这样,与node-http-proxy一起运行的正确方法是什么?



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



I也注意到这个其他模块叫做Cluster,似乎是相关的,但我不知道如何:



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



虽然不想压倒,我也遇到一个叫海布似乎是相关的,但我不知道是否只是一个全面的替代使用vhosts:



https://github.com/nodejitsu/haibu



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

解决方案

我从来没有想过海u或群星。但是我确实找到了一个解决我的问题的好方法。令我吃惊的是,这实际上很简单。但是,我不太了解服务器,所以在这样做的时候,可能不是最佳的。



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



我在Node上安装了以下内容




  • Express(http://expressjs.com/)

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



然后,作为个人风格,我把所有的虚拟主机放在一个共同的目录(/ localhost)



然后,我切换Apache来监听除端口80以外的其他端口。我刚刚选择了端口9000,因为我曾经看到使用过某处。 (在httpd.conf中,将Listen 80更改为Listen 9000)。我还必须确保我的所有虚拟主机,如额外的/ httpd-vhosts.conf中定义的,都设置为基于IP的nameVirtualHost(127.0.0.1),而不是使用端口(*:80)。

$在节点端,我创建了在端口8000上听到的应用程序/服务器(也称为节点虚拟主机)(有些任意选择端口号)请参阅创建带有快捷方式的服务器的此链接: http://expressjs.com/guide.html



在我的/ localhost目录我然后创建了一个名为nodeHttpProxy.js的文件



使用node-http-proxy,在nodeHttpProxy.js中,我创建了一个侦听端口的代理服务器80.使用express,包装连接(http://www.senchalabs.org/connect/)我创建了我的虚拟主机。



nodeHttpProxy.js文件如下所示:

  //模块依赖
var httpProxy = require('/ usr / local / lib / node_modules / http-proxy / lib / node-http-proxy')
,express = require('/ usr / local / / node_modules /快递/ LIB /快递);

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

//节点主机名的数组
var nodeVhosts = [
'vhost1'
,'vhost2'
]
,host = req.header('host')
,port = nodeVhosts.indexOf主机)> -1
?8000
:9000;

//现在代理请求
proxy.proxyRequest(req,res,{
主机:主机
,port:port
});
})
.listen(80);

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

正如你所看到的,每次创建一个新的Node虚拟主机时,我都要做两件事:


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

  2. 定义一个新的快递虚拟主机使用.set方法

当然,我也必须在我的/ localhost目录中创建实际的主机路径/文件。



一旦这一切完成,我只需要运行nodeHttpProxy.js:

  node nodeHttpProxy.js 

您可能会收到一些奇怪的EACCESS错误,在这种情况下,只需以sudo方式运行。



它将侦听端口80,如果主机与nodeVhosts数组中的一个名称匹配,则会将该请求转发到端口上的该主机8000,否则它会将请求转发到端口9000上的该主机。


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.

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

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