最近要搭建一个服务器的平台,其中一部分要实现long-polling的功能,所以用到node.js服务器,但是还有许多的代码跑在apache服务器上,所以需要使用node.js作为反向代理,apache作为内部服务器。
达成效果:long-polling的代码跑在node.js服务器上,其他代码跑在apache服务器上
OS: ubuntu
操作步骤:
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端口