简体   繁体   English

Socket.IO,SSL,nginx,目录路径

[英]Socket.IO, SSL, nginx, directory path

So I use a standalone version of socket.io (server) -> I don't declare any "http server" It looks pretty much like this: 因此,我使用socket.io(服务器)的独立版本->我没有声明任何“ http服务器”,它看起来非常像这样:

var io = require('socket.io')();
io.on('connection', function(socket){});
io.listen(3001);

The problem: my website in production environment is fully HTTPS . 问题:我在生产环境中的网站完全是HTTPS When I tried to connect to it ( io.connect("https://www.mysite.com:3001") ) I got ERR_SSL_PROTOCOL_ERROR (tested in Chrome browser). 当我尝试连接到它( io.connect("https://www.mysite.com:3001") )时,我得到了ERR_SSL_PROTOCOL_ERROR(已在Chrome浏览器中测试)。

When I turned off the server, I got ERR_CONNECTION_REFUSED. 当我关闭服务器时,我得到了ERR_CONNECTION_REFUSED。 So I came to a conclusion that Node.JS cannot handle the HTTPS/SSL connection. 因此,我得出一个结论,Node.JS无法处理HTTPS / SSL连接。

My development environment is HTTP-based, so I have no problem there - it works like a charm. 我的开发环境是基于HTTP的,所以我在那里没有问题-它像一个魅力。

So, then I decided to try proxy-passing the HTTPS to HTTP connection, using nginx. 因此,然后我决定尝试使用nginx将HTTPS代理传递给HTTP连接。

AFAIK, here I have only couple of options in order to distinguish between Node.JS connection and main app connection: AFAIK,在这里我只有几个选项可以区分Node.JS连接和主应用程序连接:

a) Subdomain (~ node.mysite.com) a)子域(〜node.mysite.com)

b) Directory (~ mysite.com/node) b)目录(〜mysite.com/node)

I chose the latter option, because it felt much easier to implement. 我选择了后者,因为它易于实现。

So I added the following inside of a main "server" config (before the main app): 因此,我在主“服务器”配置中添加了以下内容(在主应用之前):

location /node {
   proxy_pass http://127.0.0.1:3001;
   proxy_redirect off;
   proxy_set_header X-Real-IP $remote_addr;
   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
   proxy_set_header X-Forwarded-Proto $scheme;
}

And then I tried connecting: io.connect("https://www.mysite.com/node") , but no matter what I get 404 (Not Found) . 然后我尝试连接: io.connect("https://www.mysite.com/node") ,但是无论我得到404(未找到) I even tried this: io.connect("https://www.mysite.com", {path : /node/}) (w and w/o slashes) and tried renaming path to resource and many more. 我什至尝试了这一点: io.connect("https://www.mysite.com", {path : /node/}) (不带斜杠),并尝试重命名资源的 路径等等。

But no avail. 但无济于事。

When I visit this page in the browser (and when the server is up and running) - I see a white page. 当我在浏览器中访问此页面时(以及服务器启动并运行时)-我看到一个白页。

I'm I doing something terribly wrong? 我做错了什么吗? Can this be fixed? 这个可以解决吗? Thank you all in advance 谢谢大家

I also used the socket.IO connection on SSL like this .. it work fine to me ...i am using Express Engine 我也像这样在SSL上使用了socket.IO连接..它对我来说很好...我正在使用Express Engine

 var options = {
  key: fs.readFileSync('./key.pem', 'utf8'),
  cert: fs.readFileSync('./server.crt', 'utf8')
 };

 var app = require('./app');
 var server = require('https').createServer(options, app),
 io = require('socket.io').listen(server);
 server.listen(port);
 io.sockets.on('connection', function (socket) {
 });

and on Client side 在客户端

<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect('https://localhost:2406');
// on connection to server, ask for user's name with an anonymous callback
socket.on('connect', function(){});
</script>

Decided to go with a subdomain. 决定使用子域。 Because I wanted to delegate the connection handling to Nginx instead of doing it with the Node.JS itself. 因为我想将连接处理委托给Nginx而不是使用Node.JS本身来完成。 :) :)

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

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