LoginSignup
10
9

More than 5 years have passed since last update.

Apache + node-http-proxy(v1.0)でwebsocket対応のリバースプロキシを作る

Posted at

簡易版です。https://gist.github.com/novi/1830085
を参考にさせていただきました。

httpd.conf
Listen 8081

NameVirtualHost *:8081

<VirtualHost *:8081> 
  DocumentRoot /path/
  ServerName www.example1.com
  ServerAlias example1.com *.example1.com
</VirtualHost>

<VirtualHost *:8081>
  DocumentRoot /path/
  ServerName www.example2.com
  ServerAlias example2.com *.example2.com
</VirtualHost> 
http-proxy.js
var http = require('http');
var httpProxy = require('http-proxy');

var normalProxy = new httpProxy.createProxyServer({
    target: {
    host: 'localhost',
    port: 8081
}});

var webSocketProxy = new httpProxy.createProxyServer({
    target: {
    host: 'localhost',
    port: 3000
}});

var server = http.createServer(function ( req, res ) {
    if (req.headers.host == 'example1.com') {
        normalProxy.web( req, res );
    } else if (req.headers.host == 'example2.com') {
        webSocketProxy.web( req, res );
    } else {
        res.writeHead(404);
        res.end();
    }
});

server.on( 'upgrade', function( req, socket, head ) {
    webSocketProxy.ws( req, socket, head );
});
server.listen(80);
console.log('It Works!');
10
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
9