how to put nodejs and apache in the same port 80

前端 未结 6 1187
执笔经年
执笔经年 2020-12-04 17:04

I have to put nodejs in port 80, but apache is already using it. How can I put both (nodejs and apache) on the same port 80? I need it because in my university all the ports

6条回答
  •  盖世英雄少女心
    2020-12-04 17:24

    I do this via node.js proxy..

    Install http-proxy with npm or official page

    Example:

    var http = require('http'),
    httpProxy = require('http-proxy'),
    proxyServer = httpProxy.createServer ({
        hostnameOnly: true,
        router: {
            'domain.com':       '127.0.0.1:81',
            'domain.co.uk':     '127.0.0.1:82',
            '127.0.0.1':        '127.0.0.1:83'
        }
    });
    
    proxyServer.listen(80);
    

    This creates a node process listening to port 80, and forwarding requests for domains which go to :81,82,83 etc. I recommend running this with forever and adding an entry to init.d so your proxy is up in case system shuts down.

提交回复
热议问题