我正在开发一个Nodejs应用程序和托管在AWS ubuntu实例与pm2和Nginx. 应用程序出了问题,所以我检查了pm2 logs
,发现了这个
0|index | Error: listen EACCES: permission denied 0.0.0.0:300
0|index | at Server.setupListenHandle [as _listen2] (node:net:1723:21)
0|index | at listenInCluster (node:net:1788:12)
0|index | at Server.listen (node:net:1876:7)
0|index | at Function.listen (/home/ubuntu/twitter-helper/nodejs/node_modules/express/lib/application.js:635:24)
0|index | at Object.<anonymous> (/home/ubuntu/twitter-helper/nodejs/index.js:353:5)
0|index | at Module._compile (node:internal/modules/cjs/loader:1254:14)
0|index | at Module._extensions..js (node:internal/modules/cjs/loader:1308:10)
0|index | at Module.load (node:internal/modules/cjs/loader:1117:32)
0|index | at Module._load (node:internal/modules/cjs/loader:958:12)
0|index | at Object.<anonymous> (/usr/lib/node_modules/pm2/lib/ProcessContainerFork.js:33:23) {
0|index | code: 'EACCES',
0|index | errno: -13,
0|index | syscall: 'listen',
0|index | address: '0.0.0.0',
0|index | port: 300
0|index | }
问题是,我不使用端口300在我的应用程序的任何地方,所以我不知道这个端口号来自哪里,如何解决它?
我的nodejs代码:
const PORT = process.env.PORT || 3001
app.listen(PORT, ()=>{console.log(`listen on port ${PORT}`)})
Nginx配置:
server {
listen 80;
listen [::]:80;
root /home/ubuntu/app/react/frontend/build;
# Add index.php to the list if you are using PHP
index index.html index.htm index.nginx-debian.html;
server_name something something here;
location / {
try_files $uri /index.html;
}
location /api {
proxy_pass http://127.0.0.1:3001;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
非特权用户(非root)无法打开1024以下端口的监听套接字
不幸的是,除非你以root身份登录,否则你通常必须使用像 这样的URL。http://ip:port -其中端口号>1024 .
检查这个https://www.digitalocean.com/community/tutorials/how-to-use-pm2-to-setup-a-node-js-production-environment-on-an-ubuntu-vps#give-safe-user-permission-to-use-port-80
使用这个命令
sudo apt-get install libcap2-bin
sudo setcap cap_net_bind_service=+ep `readlink -f `which node``