免責聲明

Disclaimer (免責聲明)
繼續閱覽代表您接受以上的免責聲明.
To continue reading means you accept the above disclaimer.

2014年11月25日 星期二

如何讓 nodejs 與 apache 共用 port 80 ?

[Q] nodejs 無法在port 80 執行?
[Q] why does nodejs listening to port 80 cause error  :listen EACCES?

events.js:72
        throw er; // Unhandled 'error' event
              ^
Error: listen EACCES
    at errnoException (net.js:901:11)
    at Server._listen2 (net.js:1020:19)
    at listen (net.js:1061:10)


""" ...
The error code EACCES means you don't have proper permissions to run applications on that port.
On Linux systems, any port below 1024 requires root access.

... """

[ref]
http://stackoverflow.com/questions/18947356/node-js-app-cant-run-on-port-80-even-though-theres-no-other-process-blocking-t


[try]
0. run nodejs with sudo,  --> 恐有資安風險?
  $ sudo nodejs httpsvr.njs

1. use apache as reverse proxy  --> 效能較弱?
[ need to install/enable apache proxy, proxy_http module ]
 $ sudo a2enmod proxy
 $ sudo a2enmod proxy_http

<VirtualHost *:80>
ServerName subdomain.yourdomain.com
ProxyRequests Off

#ProxyPreserveHost on
ProxyPass / http://localhost:3000/
</VirtualHost>

""" ...
Every request that comes in through Apache will cause an Apache thread to
wait/block until the response is returned from your Node.js process.

... """

2. use nodejs package http-proxy to redirect port 80 traffic
-->  http-proxy 尚未穩定?
--> still needs sudo to run the proxy listening to port 80?
$ npm install  http-proxy

myproxy.njs

var http = require('http');
httpProxy = require('http-proxy');
proxy = httpProxy.createProxy(options);
var options={
    hostnameOnly: true,
    router: {
        'urdomain.com':       'http://127.0.0.1:1001',
        'nodejs.urdomain.com':     'http://127.0.0.1:1002',
        '127.0.0.1':        'http://127.0.0.1:1003'
    }
};

http.createServer(function(req, resp) {
  proxy.web(req, resp, {
    target: options.router[req.headers.host]
  }).listen(80);

xxx target : '127.0.0.1:1001'
--> target: 'http://127.0.0.1:1001'

--> 'nodejs.urdomain.com':     'http://127.0.0.1:1002',
run the actual httpsvr.njs on localhost at port 1002

3. use Nginx as reverse proxy ...

[ref]
http://www.chrisshiplet.com/2013/how-to-use-node-js-with-apache-on-port-80
http://httpd.apache.org/docs/2.2/mod/mod_proxy.html
http://stackoverflow.com/questions/11172351/how-to-put-nodejs-and-apache-in-the-same-port-80

http://blog.nodejitsu.com/http-proxy-intro/



沒有留言:

張貼留言