[英]NGINX config with Node.js Express and TCP server on same port?
I am trying to set up my node server that uses express to serve files on port 3000 and the net
library to serve a TCP server on port 5052, so: 我试图设置我的节点服务器,该节点服务器使用express在端口3000上提供文件,并使用
net
库在端口5052上提供TCP服务器,因此:
const express = require('express');
const app = express();
const httpServer = require('http').Server(app);
const io = require('socket.io').listen(httpServer);
const path = require('path');
const net = require('net');
app.get('/', (req, res) => {
res.sendFile(path.join(__dirname, './public/index.html'))
});
let server = net.createServer(function(socket) {
// Left out for brevity
}
server.listen(5052, 'localhost');
httpServer.listen(3000, () => {
console.log('Ready on port 3000');
});
Locally, this all works very well. 在本地,这一切都很好。 I can load up
localhost:3000
and I get my HTML served and it connects to socket.io fine. 我可以加载
localhost:3000
并且可以获取HTML并将其连接到socket.io。 I can also connect to the server on port 5052 perfectly and life is good. 我还可以完美地连接到端口5052上的服务器,并且寿命长。 I just can't get nginx to serve it all correctly.
我只是无法让Nginx正确地提供所有服务。 Here's what I have:
这是我所拥有的:
server {
listen 80 default_server;
listen [::]:80 default_server;
server_name mycoolproject.com www.mycoolproject.com;
location / {
proxy_pass http://localhost:3000;
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;
}
listen 443 ssl;
ssl_certificate /etc/letsencrypt/live/mycoolproject.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/mycoolproject.com/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/ssl/certs/dhparam.pem;
if ($scheme != "https") {
return 301 https://$host$request_uri;
}
}
server {
listen 5053;
server_name mycoolproject.com www.mycoolproject.com;
location /{
proxy_pass http://localhost:5052;
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;
}
}
When I navigate to mycoolproject.com
I get the site loaded fine, so the express side is working fine. 当我导航到
mycoolproject.com
我可以很好地加载网站,因此快速端可以正常工作。 I just can't connect to my server on 5053. Any ideas? 我只是无法连接到5053上的服务器。有什么想法吗?
You need to configure a different port for Nginx, 5052 is busy by Node.js. 您需要为Nginx配置其他端口,Node.js忙于5052。
server {
listen 5053;
server_name mycoolproject.com www.mycoolproject.com;
location /{
proxy_pass http://localhost:5052;
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;
}
}
Then you can connect to mycoolproject.com:5053 然后您可以连接到mycoolproject.com:5053
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.