如何在端口 80 上运行 Node.js? [英] How do I run Node.js on port 80?

查看:48
本文介绍了如何在端口 80 上运行 Node.js?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是在端口 80 上运行 Node.js.这是因为我发现 node.js 被某些网络阻止,这些网络不允许来自任何其他端口的流量.

My aim is to run Node.js on port 80. This is because I am finding node.js is being blocked from certain networks which do not allow traffic from any other port.

似乎最好的方法是通过 Node.js 代理 Apache.我曾尝试使用 node-http-proxy 来做到这一点,但我没有任何运气.

It appears that the best way to do this is by proxying Apache through Node.js. I have tried using node-http-proxy to do this but I have not had any luck.

我使用的代码在这里:

var util = require('util'),
    http = require('http'),
    httpProxy = require('http-proxy');

httpProxy.createServer(9000, 'localhost').listen(80);

http.createServer(function (req, res) {
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.write('request successfully proxied to: ' + req.url + '\n' + JSON.stringify(req.headers, true, 2));
  res.end();
}).listen(9000);

但是我一直收到端口 80 的错误地址正在使用".我一定是做错了什么.

But I keep getting the error "Address in use" for port 80. I must be doing something wrong.

如何使用 node-http-proxy 通过 node.js 代理 Apache?这能让我在端口 80 上运行 node.js 吗?node-http-proxy 是实现这一目标的最佳方式吗?

How do I proxy Apache through node.js using node-http-proxy? Will this enable me to run node.js on port 80? And is node-http-proxy the best way to achieve this?

谢谢.

推荐答案

您需要做的是为您正在运行的服务器提供 2 个 ip.Apache 有 1 个 ip 绑定到端口 80,然后 node.js 有另一个 ip 绑定到端口 80.

What you need to do is have 2 ip's for the server you are running. Apache has 1 ip bound to port 80 and then node.js has the other ip bound to port 80.

使用节点及其监听指令有 2 个值,例如..listen(80, NODEJS_IP 或 DNS NAME);

Using node and its listen directive has 2 values eg. .listen(80, NODEJS_IP or DNS NAME);

其他一些建议.

我不会将 apache 与 nodejs 一起使用,因为它没有发生事件.所以这个真的不推荐.我实际上会考虑使用 NGINX 作为它与 Node 更好的配对.

I would not use apache with nodejs as it's not evented. So this really isn't recommended. I would actually look into using NGINX as its a much better pairing with Node.

这篇关于如何在端口 80 上运行 Node.js?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆