问题描述
和往常一样,我一直在寻找我的问题的答案,所以我再次向你们所有的天才伸出援手!:)
As always I have searched my fingers bloody finding an answer to my issue, so I am reaching out to all you geniuses again! :)
我已经使用 socket.io(使用 express)设置了一个 Node.js 服务器,它使用端口 8443 运行良好.它运行 :).由于我的许多客户似乎不允许端口 8443 上的流量,因此他们无法使用我的服务.
I have set up a Node.js server with socket.io (using express) and it runs nice using port 8443. Well it runs :). Since many of my customer doesn't seem to allow traffic on port 8443 they are not able to use my services.
我想知道如何在端口 443 上设置 Node.js,因为使用 Node-server 的站点已经在使用这个端口 (Https).如果我尝试在我的节点服务器上使用端口 443,我会得到:警告 - 引发错误:错误:监听 EACCES
I am wondering how to setup Node.js on port 443 since the site using the Node-server is already using this port (Https). If I try to use port 443 on my Node-server i get: warn - error raised: Error: listen EACCES
我的 Node-js 代码的一部分:
Part of my Node-js code:
var fs = require('fs');
var https = require('https');
var express = require('express');
var socket = require('socket.io');
var port = 8443;
var sslOptions = {
pfx: fs.readFileSync('mykey.pfx'),
passphrase: ********
};
var app = express();
app.all('/*', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With, *');
next();
});
var server = https.createServer(sslOptions, app);
var io = socket.listen(server, {
"log level" : 1,
"match origin protocol" : true,
"transports" : ['websocket', 'flashsocket'
, 'xhr-polling'
, 'jsonp-polling']
});
//No need to list all my socket events I guess
server.listen(port);
连接到我的 Node 服务器的客户端代码:
Client code to connect to my Node server:
var socket = io.connect("https://www.*MYWEBSITE*.com", { secure: true, port: 8443});
推荐答案
我想你在这里问了几个不同的问题.我会单独回答第一个,它应该可以解决您最初的问题,但其他一些人提出了一些也值得了解的方便的部署选项.
I think you've asked a couple different questions here. I'll answer the first one on it's own and it should solve your original problem, but some others have brought up a couple handy deployment options that are worth knowing too.
首先,您不需要在自己的端口上运行 socket.io 服务器.您可以将 socket.io 服务器绑定到 express 应用程序.例如:
First, you don't need to run the socket.io server on it's own port. You can just have the socket.io server bind itself to the express app. For example:
// ... require stuff
var app = express();
// ... set up your express middleware, etc
var server = https.createServer(sslOptions, app);
// attach your socket.io server to the express server
var io = require("socket.io").listen(server);
server.listen(port);
无论您如何设置节点应用程序,了解如何将 nginx 设置为反向代理都是值得的.这很好,因为您不必是 root 用户即可使用端口 <1024 运行 node 应用程序时,您可以通过虚拟主机将多个应用程序绑定到同一个 IP 地址.
Regardless of how you set up your node application it's worth knowing how to set up nginx as a reverse proxy. This is nice because you don't have to be root to use ports < 1024 when running your node app and you can bind many applications to the same IP address through virtual hosts.
这是一个 nginx 服务器配置块,它将反向代理节点应用程序(并支持 websockets).
Here's an nginx server configuration block that will reverse proxy a node application (and supports websockets).
server {
listen 80;
server_name app.com www.app.com # a list of hosts for this application
access_log /var/log/nginx/access/app.log; # you'll need to create /var/log/nginx/access
error_log /var/log/nginx/error/app.log; # and you'll need to create /var/log/nginx/error
# prevents 502 bad gateway error
large_client_header_buffers 8 32k;
location / {
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_set_header Accept-Encoding "";
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-NginX-Proxy true;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_buffers 8 32k;
proxy_buffer_size 64k;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_pass http://127.0.0.1:8000; # put the port of your node app here
proxy_redirect off;
}
}
这篇关于如何将 Socket.io 配置为在 https 上的同一端口上运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!