简体   繁体   English

Socket.io聊天应用程序,路径上的Apache反向代理

[英]Socket.io chat app, apache reverse proxy on path

I am using socket.io as a chat application using localhost:3000, this is working great in most cases, however for some users port 3000 is getting blocked and therefore the chat app is not working. 我使用socket.io作为使用localhost:3000的聊天应用程序,在大多数情况下都可以正常工作,但是对于某些用户,端口3000被阻塞,因此聊天应用程序无法正常工作。

Instead I want to use a reverse proxy to get this to load up a url with a path, but I can't get it working. 相反,我想使用反向代理来获取它来加载带有路径的URL,但是我无法使其正常工作。 I've looked at loads of examples, but none of them are working so I was really hoping someone can steer me in the right direction. 我查看了很多示例,但是它们都没有起作用,因此我真的希望有人可以引导我朝正确的方向发展。

I want to go from this: 我想从这里开始:

https://mydomain.co.uk:3000 https://mydomain.co.uk:3000

to this 对此

https://mydomain.co.uk/chatapp https://mydomain.co.uk/chatapp

This is what I have put in the default-ssl.conf 这就是我放在default-ssl.conf中的内容

<Location /chatapp>
    Order allow,deny
    Allow from all
    ProxyPass http://localhost:3000/
    ProxyPassReverse http://localhost:3000/
</Location>

However when I navigate to https://mydomain.co.uk/chatapp I am getting the following error message. 但是,当我导航到https://mydomain.co.uk/chatapp时,出现以下错误消息。

在此处输入图片说明

Is there something I need to change on my server or client side code? 我需要在服务器或客户端代码上进行更改吗?

Currently my server code starts with the following... 目前,我的服务器代码以以下内容开头...

// Setup basic express server
var fs = require('fs');
var express = require('express');
var app = express();
var path = require('path');
var server = require('https').createServer({
    key: fs.readFileSync('/etc/apache2/ssl/**********************************.key'),
    cert: fs.readFileSync('/etc/apache2/ssl/**********************************.crt'),
    ca: fs.readFileSync('/etc/apache2/ssl/**********************************.crt'),
    requestCert: false,
    rejectUnauthorized: false
}, app);
var io = require('../..')(server);
var port = process.env.PORT || 3000;

server.listen(port, function () {
    console.log('Server listening at port %d', port);
});

// Routing
app.use(express.static(path.join(__dirname, 'public')));

// Chatroom

var numUsers = 0;

io.on('connection', function (socket) {
    var addedUser = false;

My client code just creates the socket like this... 我的客户代码只是像这样创建套接字...

var socket = io();

For anyone interested, I found the solution. 对于任何有兴趣的人,我都找到了解决方案。

The problem was that my socket.io was configured to only work over https, therefore when the reverse proxy was trying to connect, it was trying to go to http://localhost:3000 and it was failing. 问题是我的socket.io配置为仅通过https工作,因此,当反向代理尝试连接时,它试图转到http:// localhost:3000 ,并且失败。

The fix was to change this... 解决方法是更改​​此...

var server = require('https').createServer({
key: fs.readFileSync('/etc/apache2/ssl/**********************************.key'),
cert: fs.readFileSync('/etc/apache2/ssl/**********************************.crt'),
ca: fs.readFileSync('/etc/apache2/ssl/**********************************.crt'),
requestCert: false,
rejectUnauthorized: false
}, app);

To this... 为此...

var server = require('http').createServer(app);

The proxy then kicked into life and started working. 然后,代理人开始活跃起来并开始工作。

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

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