简体   繁体   中英

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.

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. 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

to this

https://mydomain.co.uk/chatapp

This is what I have put in the 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.

在此处输入图片说明

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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