Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
456 views
in Technique[技术] by (71.8m points)

node.js - how to put nodejs and apache in the same port 80

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 are blocked except for PORT 80. (This is a realtime application with nodejs and socket.io (websockets) and in the other side a php application). Thanks a lot

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...