如何与 node-http-proxy 一起使用 vhosts? [英] How to use vhosts alongside node-http-proxy?

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

问题描述

我同时运行 Nodejs 和 Apache.

I'm running Nodejs and Apache alongside each other.

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

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

我在 Apache 上的虚拟主机看起来像:

My virtual hosts on Apache look like:

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

我的问题是,在 Express/Nodejs 端拥有类似 vhost 的功能的正确"方法是什么?我不想像这里建议的那样将每个 Nodejs 应用程序放在自己的端口上:

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(标题为使用‘仅主机名’代理表的代理请求"的部分)

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

我注意到 Connect(据我所知,它捆绑在 Express 中)具有一些 vhosts 功能.我应该使用它吗?如果是这样,将它与 node-http-proxy 一起运行的正确方法是什么?

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

推荐答案

我从来没有想过 Haibu 或 Cluster.但我确实找到了一个很好的解决方案来解决我的问题.令我惊讶的是,这实际上非常简单.但是,我对服务器了解不多,所以虽然这可行,但可能不是最佳选择.

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.

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

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

我在 Node 上安装了以下内容

I installed the following on Node

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

然后,出于个人风格,我将所有虚拟主机放在一个公共目录 (/localhost) 中

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

然后我将 Apache 切换为侦听端口 80 以外的端口.我只是碰巧选择了端口 9000,因为我在某处看到过该端口.(在 httpd.conf 中,将Listen 80"更改为Listen 9000").我还必须确保将 extra/httpd-vhosts.conf 中定义的所有虚拟主机设置为基于 IP 的 nameVirtualHost (127.0.0.1),而不是使用端口 (*:80).

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

在节点端,我创建了我的应用程序/服务器(又名节点虚拟主机),它侦听端口 8000(端口号的任意选择)请参阅有关使用 express 创建服务器的链接:http://expressjs.com/guide.html

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

在我的/localhost 目录中,我创建了一个名为nodeHttpProxy.js"的文件

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

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

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.

nodeHttpProxy.js 文件如下所示:

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

如您所见,每次创建新的 Node 虚拟主机时,我都必须做两件事:

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

  1. 将虚拟主机名添加到我的nodeVhosts"数组中
  2. 使用 .set 方法定义一个新的 express 虚拟主机

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

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

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

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

node nodeHttpProxy.js

您可能会遇到一些奇怪的EACCESS"错误,在这种情况下,只需以 sudo 身份运行即可.

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

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

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 一起使用 vhosts?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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