简体   繁体   English

在子位置运行nginx后面的socket.io

[英]Running socket.io behind nginx at a sublocation

I am trying to run an express-socket.io-angular.js Application on a raspberry my at my home. 我试图在我家的树莓上运行express-socket.io-angular.js应用程序。

Express and socket.io are running on port 3001. Accessing the Application at http://[IP]:3001 works fine. Express和socket.io正在端口3001上运行。在http:// [IP]:3001上访问应用程序正常。 I would like to access the application at http://[IP]/ttt . 我想访问http:// [IP] / ttt的应用程序。

I configured nginx like this: 我像这样配置了nginx:

location /ttt/ {
    proxy_pass http://localhost:3001/;
}

On the server i run these lines of code: 在服务器上,我运行以下代码行:

var express = require('express');
var app = express();
var http = require('http').Server(app);

...

app.use(express.static(__dirname + '/public'));
app.use('/node_modules', express.static(__dirname + '/node_modules'));

...

var io = require('socket.io')(http, {path: '/ttt/socket.io'});

...

http.listen(3001, function () {
    console.log('Listening on port 3001');
});

and at the client I include socket.io like this: 在客户端我包括socket.io像这样:

<script src="socket.io/socket.io.js"></script>

and connect like this: 并像这样连接:

socket: Socket = io.connect({path: "/ttt/socket.io"});

The problem: 问题:

I get all resources (html, css, js incl. socket.io.js), but in the browserlog i get error messages, that it was not able to get http://[IP]/socket.io/?EIO=3&transport=polling&t=LNGvDUd where t changes frequently. 我获得了所有资源(html,css,js,包括socket.io.js),但在浏览器日志中我收到错误消息,它无法获取http:// [IP] /socket.io/?EIO= 3&transport = polling&t = LNGvDUd ,其中t经常变化。

It reports status 404, it's obvious, because nginx doesn't respond at /socket.io. 它报告状态404,很明显,因为nginx在/socket.io没有响应。

Thanks in advance! 提前致谢!


Combined results with jfriend00: 结合jfriend00的结果:

The working setup looks like this: 工作设置如下所示:

nginx (found more details about the correct configuration here ): nginx(在这里找到有关正确配置的更多细节):

location /ttt/ {
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_http_version 1.1;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;
        proxy_pass http://localhost:3001/;
    }

Express: 表达:

var io = require('socket.io')(http);

Client (Typescript): 客户(打字稿):

socket: Socket = io({path: "/ttt/socket.io"});

If I understand your proxy configuration correctly, it is going to strip off the /ttt from the path before forwarding on to port 3001. As such, your socket.io initialization is not correct on the server. 如果我正确理解您的代理配置,它将在转发到端口3001之前从路径中剥离/ttt 。因此,您的socket.io初始化在服务器上是不正确的。

Change this: 改变这个:

var io = require('socket.io')(http, {path: '/ttt/socket.io'});

to this: 对此:

var io = require('socket.io')(http);

EDIT: 编辑:

And, make sure that your client file that's loading into the browser is actually the latest edited client file. 并且,确保加载到浏览器中的客户端文件实际上是最新编辑的客户端文件。 You can check it with View/Source in the browser to see exactly what file you're running. 您可以在浏览器中使用“查看/来源”进行检查,以确切了解您正在运行的文件。

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

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