虛擬主機代理

一、通過向HTTP請求中添加"Host:"頭來實現(xiàn)虛擬主機。

當訪問某個網(wǎng)站的時候通過修改host請求頭來實現(xiàn)訪問不同的頁面

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利用多核優(yōu)勢實現(xiàn)代理

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

三、循環(huán)代理均衡器

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

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,661評論 19 139
  • 一、概念(載錄于:http://www.cnblogs.com/EricaMIN1987_IT/p/3837436...
    yuantao123434閱讀 8,744評論 6 152
  • 上一篇《WEB請求處理一:瀏覽器請求發(fā)起處理》,我們講述了瀏覽器端請求發(fā)起過程,通過DNS域名解析服務器IP,并建...
    七寸知架構閱讀 81,789評論 21 356
  • #文# “此地無銀三百兩” 【吼吼吼,一篇略甜的基佬文】 ——很久很久很久以前,有個很有錢很有錢的少年, 和...
    若修大人閱讀 525評論 0 1
  • 記錄 18.03.17-03.18 天竺山10K徒步-晉江三山徒步越野 首探海滄天竺山 同事六人,晨起6點,從公司...
    追光者M閱讀 169評論 0 0

友情鏈接更多精彩內(nèi)容