How to use vhosts alongside node-http-proxy?

前端 未结 4 517
太阳男子
太阳男子 2020-11-29 06:54

I\'m running Nodejs and Apache alongside each other.

node-http-proxy is listening on port 80 and then forwarding requests to either Apache(:9000) or to Express(:8000

4条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-29 07:29

    I've been giving this some thought lately as I'm tackling the same problems on my personal test environment. You are not going to be able to get around having each node application running on it's own port, but you can abstract away the pain of that process. Here is what I am using now, but I hope to build an npm package around this to simplify things in the future.

    Each of my node.js applications has a map file that contains the port that the application is listening on as well as a map that indicates the expected path which the application is being served on. The contents of the file look like this:

    {"path": "domain.com/path", "port": 3001}
    

    When I start my application, it will read the port from the map.json file and listen on the specified port.

    var map = fs.readFileSync('map.json', 'ascii');
    app.listen(map.port);
    

    Then in my proxy setup, I iterate over each of my node.js application directories, and check for a map.json file which indicates port 80 traffic should be proxied to this application.

    I use almost the exact same method to setup the proxy for our apache hosted applications as well. We use a folder based convention on the PHP websites that we are serving and it uses the following configuration:

    VirtualDocumentRoot /var/www/%-2.0.%-1/%-3+/
    VirtualScriptAlias /var/www/%-2.0.%-1/%-3+/cgi-bin/
    

    This essentially allows us to map domains to folders using the following structure.

    http://sub.domain.com/ = /var/www/domain.com/sub/
    

    There is no additional configuration needed to add or remove sites. This is very close to what I am currently using to proxy both apache and node sites. I am able to add new node and new apache sites without modifying this proxy application.

    proxy.js

    var fs = require('fs');
    var httpProxy = require('http-proxy');
    
    var proxyTable = [];
    
    // Map apache proxies
    fs.readdirSync('/var/www/').forEach(function(domain) {
        fs.readdirSync('/var/www/' + domain).forEach(function(path) {
            var fqd = domain + '/' + path;
            var port = fs.readFileSync('port', 'ascii');
            proxyTable[fqd] = fqd + ':' + 8080;
        });
    });    
    
    // Map node proxies
    fs.readdirSync('/var/www-node/').forEach(function(domain) {
            var map = fs.readFileSync('map.json', 'ascii');
            proxyTable.[map.path] = '127.0.0.1:' + map.port;
    });
    
    var options = {
        router: proxyTable
    };
    
    var proxyServer = httpProxy.createServer(options);
    proxyServer.listen(80);
    

    In the future, I will probably decouple the path from the port that the application is listening on, but this configuration allows me to build the proxy map automatically with very little work. Hopefully this helps.

提交回复
热议问题