简体   繁体   English

什么是生产环境中node.js推荐端口的最佳实践?

[英]What is best practice node.js recommended port in production environment?

I have created a chat app in node.js using port 4000. Everything works just fine, but when I rolled it out in production, I found that many corporate networks block outgoing port 4000. I considered using other ports that would be more likely to be open on a corporate network, but then found this list of ports blocked by chrome browser: 我已经使用端口4000在node.js中创建了一个聊天应用程序。一切正常,但是当我在生产中推出它时,我发现许多公司网络阻止了传出端口4000.我考虑使用更有可能的其他端口在公司网络上打开,但后来发现这个端口列表被Chrome浏览器阻止:

https://superuser.com/questions/188058/which-ports-are-considered-unsafe-by-chrome https://superuser.com/questions/188058/which-ports-are-considered-unsafe-by-chrome

Using ports such as 995 would result in a chrome error of "ERR_UNSAFE_PORT" 使用995之类的端口会导致chrome错误“ERR_UNSAFE_PORT”

So it appears that the only ports allowed are 80 and 443 for a node.js server? 因此,对于node.js服务器,似乎唯一允许的端口是80和443? What is the recommended best practice for choosing a port for your node.js application in a production environment? 在生产环境中为node.js应用程序选择端口的建议最佳做法是什么?

My webserver is already using ports 80 and 443 for typical apache web serving. 我的网络服务器已经使用端口80和443进行典型的apache web服务。 Do I need to create a dedicated server just for node.js? 我是否需要为node.js创建专用服务器?

I am using the following code to initiate the connection from the browser to the node.js server: 我使用以下代码启动从浏览器到node.js服务器的连接:

var socket = io.connect('https://duplex.example.com:4000');

and here is the code on the server side: 这是服务器端的代码:

const https = require('https');
const fs = require('fs');
var express = require('express')
  , bodyParser = require('body-parser');
var socket = require('socket.io');
var adminid = '';
var clientlist = new Array();
var port = 4000;

const options = {
    cert: fs.readFileSync('./fullchain.pem'),
    key: fs.readFileSync('./privkey.pem')
};

var app = express();

var server = https.createServer(options, app).listen(port, function(){
  console.log("Express server listening on port " + port);
});

443 and 80 are the main ports for https and HTTP traffic respectively. 443和80分别是https和HTTP流量的主要端口。

other ports can be used for WebSockets, but that doesn't sound like your use case. 其他端口可用于WebSockets,但这听起来不像您的用例。

What I have done in the past is use a reverse proxy, to discriminate on the incoming URL, and map the ports internally on my machine without the client needing to know. 我过去所做的是使用反向代理,区分传入的URL,并在我的机器内部映射端口,而无需客户端知道。

NGINX is usually the easiest bet for this if you are on any sort of linux distro. 如果您使用任何类型的Linux发行版,NGINX通常是最简单的选择。

here is a blog about how to setup reverse proxy for a node app using nginx. 这是一个关于如何使用nginx为节点应用程序设置反向代理的博客。

http://thejonarnold.com/configure-sails-js-with-subdomains-on-ubuntu/ http://thejonarnold.com/configure-sails-js-with-subdomains-on-ubuntu/

the article references sailsjs, but there is nothing framework specific about the techique. 文章引用了sailsjs,但没有关于技术的具体框架。

Most people don't expose their Node.js server directly to the internet but use Apache or Nginx as a frontend proxy. 大多数人不会将他们的Node.js服务器直接暴露给互联网,而是使用Apache或Nginx作为前端代理。

  1. Have your server bind to localhost only (or use firewall rules to only allow incoming 80 and 443. 让您的服务器仅绑定到localhost (或使用防火墙规则仅允许传入80和443。

     server.listen('localhost', 4000) 
  2. Configure your reverse proxy. 配置反向代理。 I'm using Caddy : 我正在使用凯迪

     example.com { root /var/www/example.com # et cetera } duplex.example.com { proxy / localhost:4000 { websocket } } 

    When proxying websocket, you need to ensure the Connection and Upgrade headers aren't lost, which I've done with Caddy's shortcut here. 代理websocket时,您需要确保ConnectionUpgrade标头不会丢失,我已经在这里使用了Caddy的快捷方式。

    You could also use the same domain as the main site and only proxy a certain path. 您也可以使用与主站点相同的域,并仅代理某个路径。

  3. Have the client socket.io connect to wss://duplex.example.com (on port 443). 让客户端socket.io连接到wss://duplex.example.com (在端口443上)。 (I'm not familiar with socket.io to say why it uses an HTTPS URL instead of WSS, but I'll assume you have that working.) (我不熟悉socket.io,说它为什么使用HTTPS URL而不是WSS,但我会假设你有这个工作。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM