node.js 做反向代理apache作为内部服务器分流
2012-11-14 10:12
429 查看
最近要搭建一个服务器的平台,其中一部分要实现long-polling的功能,所以用到node.js服务器,但是还有许多的代码跑在apache服务器上,所以需要使用node.js作为反向代理,apache作为内部服务器。
达成效果:long-polling的代码跑在node.js服务器上,其他代码跑在apache服务器上
OS: ubuntu
操作步骤:
1. 安装mysql,apache,php
sudo apt-get install mysql-server
sudo apt-get install apache2
sudo apt-get install php5
sudo apt-get install libapache2-mod-php5
apt-cache search php5
sudo apt-get install php5-mysql php5-curl php5-gd php5-intl php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-ming php5-ps php5-pspell php5-recode
php5-snmp php5-sqlite php5-tidy php5-xmlrpc php5-xsl
重启 Apache2:
apache2中将/etc/apache2/ports.conf中第一行(80端口行)注释掉,并添加listen 8000(自己想要的端口)
设置/etc/apache2/sites-available/mysite为
sudo a2ensite mysite(激活)
sudo a2enmod rewrite (使能rewrite.mod模块)
/etc/init.d/apache2 restart
2. 安装node.js
sudo apt-get install curl
sudo apt-get install libssl-dev
sudo apt-get install g++
git clone git://github.com/joyent/node.git
cd node
./configure
make
sudo make install
3. node-http-proxy(需要npm来安装)
sudo curl https://npmjs.org/install.sh | sh
npm install http-proxy
4. 编写代理代码proxy.js
sudo node proxy.js (80端口必须使用sudo权限)
5. 经过以上4步,就可以将10.3.3.1/123:80的请求转发到8124端口,其余的转发的8000端口
达成效果:long-polling的代码跑在node.js服务器上,其他代码跑在apache服务器上
OS: ubuntu
操作步骤:
1. 安装mysql,apache,php
sudo apt-get install mysql-server
sudo apt-get install apache2
sudo apt-get install php5
sudo apt-get install libapache2-mod-php5
apt-cache search php5
sudo apt-get install php5-mysql php5-curl php5-gd php5-intl php-pear php5-imagick php5-imap php5-mcrypt php5-memcache php5-ming php5-ps php5-pspell php5-recode
php5-snmp php5-sqlite php5-tidy php5-xmlrpc php5-xsl
重启 Apache2:
apache2中将/etc/apache2/ports.conf中第一行(80端口行)注释掉,并添加listen 8000(自己想要的端口)
设置/etc/apache2/sites-available/mysite为
<VirtualHost *:8000> ServerAdmin webmaster@localhost DocumentRoot /home/user/web # firstProject # This should be omitted in the production environment #SetEnv APPLICATION_ENV development <Directory /> Options FollowSymLinks AllowOverride None Order deny,allow allow from all </Directory> Alias /api /home/yren/api <Directory /home/yren/api> Options FollowSymLinks AllowOverride None Order deny,allow allow from all #这个要小心,是用来设定重定向的,自己写自己的重定向规则!!!! <IfModule mod_rewrite.c> RewriteEngine on RewriteBase /api RewriteRule . index.php </IfModule> </Directory> ErrorLog /var/log/apache2/error-8000.log # Possible values include: debug, info, notice, warn, error, crit, # alert, emerg. LogLevel debug CustomLog /var/log/apache2/access-8000.log combined <IfModule mod_rewrite.c> RewriteEngine on RewriteLog /var/log/apache2/rewrite-8000.log RewriteLogLevel 9 </IfModule> </VirtualHost>
sudo a2ensite mysite(激活)
sudo a2enmod rewrite (使能rewrite.mod模块)
/etc/init.d/apache2 restart
2. 安装node.js
sudo apt-get install curl
sudo apt-get install libssl-dev
sudo apt-get install g++
git clone git://github.com/joyent/node.git
cd node
./configure
make
sudo make install
3. node-http-proxy(需要npm来安装)
sudo curl https://npmjs.org/install.sh | sh
npm install http-proxy
4. 编写代理代码proxy.js
var http = require('http'), httpProxy = require('./node_modules/http-proxy/lib/node-http-proxy'); //这个目录一定要写正确 //httpProxy.createServer(8124, 'localhost').listen(80); var options = { router: { '10.3.3.1/123': '127.0.0.1:8124', '10.3.3.1': '127.0.0.1:8000' } }; var proxyServer = httpProxy.createServer(options); proxyServer.listen(80); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello Node.jsn'); }).listen(8124); console.log('Server running at http://127.0.0.1:8124/');
sudo node proxy.js (80端口必须使用sudo权限)
5. 经过以上4步,就可以将10.3.3.1/123:80的请求转发到8124端口,其余的转发的8000端口
相关文章推荐
- Node.js学习笔记【1】入门(服务器JS、函数式编程、阻塞与非阻塞、回调、事件、内部和外部模块)
- 详解本地Node.js服务器作为api服务器的解决办法
- Node.js作为服务器的文件上传
- 用Node.js开发memcache协议的反向代理服务器
- 【Node】node.js实现服务器的反向代理,解决跨域问题
- Node.js静态文件服务器改进版
- 用node.js替代nginx做反向代理
- Apache服务器的配置具体操作—— Apache比较强大,所以要配置Apache作为本地web服务器
- 零基础搭建Node.js、Express、Ejs、Mongodb服务器及应用开发入门
- 使用Nodejs+mongodb开发地图瓦片服务器
- 详解把Node.js项目部署到阿里云服务器(CentOs)
- 解决node.js安装cnpm、grunt、bower出现的不是内部或外部命令问题
- Node.js开发入门—HTTP文件服务器
- Node.js http服务器搭建和发送http的get,post请求
- Node.js简介:可独立于浏览器,运行于服务器的Javascript
- Node.js权威指南 (8) - 创建HTTP与HTTPS服务器及客户端
- Node.js的简易服务器环境的搭建
- Node.js实战 建立简单的Web服务器
- 突袭HTML5之WebSocket入门2 - 高效服务器Node.js
- (pomelo系列入门教程)深入浅出Node.js游戏服务器开发--分布式聊天服务器搭建