节点:端口 443 需要提升权限错误

Node: PORT 443 requires elevated privileges error

我正在使用 PM2 启动应用程序,并在启动应用程序时将 PORT=443 作为参数传递。但是,它 returns 并显示错误 "PORT 443 requires elevated privileges"。虽然我已经使用 openssl 生成了证书和密钥并在代码中引用了。感谢您的支持

#!/usr/bin/env node

var app = require('../app');
var debug = require('debug')('ls-templates-server:server');
var https = require('https');
var fs = require('fs');
/**
 * Get port from environment and store in Express.
 */

var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);


var options = {
  key: fs.readFileSync('/home/admin/cert/server.key'),
  cert: fs.readFileSync('/home/admin/cert/server.cert')
};

var httpsServer = https.createServer(options, app);

/* for https (production stage) */

httpsServer.listen(port, "0.0.0.0");
httpsServer.on('error', onError);
httpsServer.on('listening', onListening);


/**
 * Normalize a port into a number, string, or false.
 */

function normalizePort(val) {
  var port = parseInt(val, 10);

  if (isNaN(port)) {
    // named pipe
    return val;
  }

  if (port >= 0) {
    // port number
    return port;
  }

  return false;
}


/**
 * Event listener for HTTP server "listening" event.
 */

function onListening() {
  var addr = httpsServer.address();
  var bind = typeof addr === 'string'
    ? 'pipe ' + addr
    : 'port ' + addr.port;
  debug('Listening on ' + bind);
}

选项 1 .

运行 PM2 作为 sudo

如果不行,用authbind

实现
sudo apt-get install authbind
sudo touch /etc/authbind/byport/443
sudo chown %user% /etc/authbind/byport/443
sudo chmod 755 /etc/authbind/byport/443

编辑 ~/.bashrc 文件并添加

+alias pm2='authbind --deep pm2'

在文件末尾,运行

source ~/.bashrc

最后确保 pm2 使用 authbind 更新:

authbind --deep pm2 update

选项 2

使用不同的 PORT 并使用 Nginxreverse proxy 您的应用程序

例如:将您的端口更改为 3000

在 Nginx 中,创建一个将请求转发到您的应用程序的服务器块。

服务器块例如:

server {
    #listen [::]:80;

    server_name your-domain.com

    #root /var/www/example.com;
    #index index.html;

        client_max_body_size 20M;

    location / {
        proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_set_header X-NginX-Proxy true;

      proxy_pass http://localhost:3000;
      proxy_redirect off;

      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "Upgrade";
      proxy_connect_timeout       500000;
      proxy_send_timeout          500000;
      proxy_read_timeout          500000;
      send_timeout                500000;
    }



        listen 443 ssl;
        ssl_certificate /home/admin/cert/server.cert;
        ssl_certificate_key /home/admin/cert/server.key;


        if ($scheme = http) {
        return 301 https://$server_name$request_uri;
        }



}

最好使用Nginx / Authbind方法,最好运行作为特权用户尽可能少,因为你想限制潜在的损害,以防有人利用你的程序。你不想 运行 你的 Node 代码作为 root 除非你绝对必须这样做。

参考 https://pm2.keymetrics.io/docs/usage/specifics/#listening-on-port-80-w-o-root

https://www.digitalocean.com/community/tutorials/how-to-use-pm2-to-setup-a-node-js-production-environment-on-an-ubuntu-vps