我爱编程

虚拟主机代理

2018-04-16  本文已影响0人  不睡觉呀

一、通过向HTTP请求中添加"Host:"头来实现虚拟主机。

当访问某个网站的时候通过修改host请求头来实现访问不同的页面

var express = require('express');

var one = express();
one.get("/",function(req,res){
    res.end("this is app one ");
});

//app two 
var two = express();
two.get("/",function(req,res){
    res.end('this is app two');
});

//app three
var three = express();
three.get("/",function(req,res){
    res.end("this is app three");
});

//controlling app
var master_app = express();

master_app.use(express.logger('dev'))
.use(express.vhost('app1',one))

.use(express.vhost('app2',two))
.use(express.vhost("app3",three))

.listen(8080);

二、使用HTTPProxy利用多核优势实现代理

var httpProxy = require('http-proxy');

var options = {
    hostnameOnly:true,
    router:{
        "app1":"192.168.1.111:8081",
        "app2":"192.168.1.111:8082",
        "app3":"192.168.1.111:8083",
    }
}

var proxyServer = httpProxy.createServer(options);
proxyServer.listen(8080);

三、循环代理均衡器

var httpProxy = require("http-proxy");
var fs = require("fs");

var server = JSON.parse(fs.readFileSync("server_list.json")).server;

var s = httpProxy.createServer(function(req,res,proxy){
    var target = server.shift();
    proxy.proxyRequest(req,res,target);
    server.push(target);
});

s.listen(8080);
{
    "server":[
        {
            "host":"localhost",
            "port":"9600"
        },
        {
            "host":"localhost",
            "port":"9601"
        },
        {
            "host":"localhost",
            "port":"9602"
        }
    ]
}

四、memcached

var express = require("express");
var MemcachedStore = require('connect-memcached')(express);
var mcds = new MemcachedStore({hosts:"192.168.1.111:11211"});

var app = express();
app.use(express.logger('dev'))
.use(express.cookieParser())
.use(express.session({
    secret:"secret",
    cookie:{maxAge:18000000},
    store:mcds
}))
.use(function(req,res){
    var x = req.session.last_access;
    req.session.last_access = new Date();
    res.end("you las ashed for this page at :"+x);
})
.listen(8080);

上一篇下一篇

猜你喜欢

热点阅读