-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Websockets not working with HTTPS in an Apache Proxy (302 error?) #893
Comments
Yeah this is most likely the issue, but the question is: why the proxy answer with a 302? |
Can you try to remove this directive?
|
Thanks @lpinca, tried it, but I got the same results. |
Is there something else I can do? I won't like to switch to a pulling mechanism in my app, but I need to get websockets to work in a HTTPS scenario. If I setup ws as a standalone service (not tied to express) I'm afraid I won't be able to bind the application user+session with the websocket connection. |
What is the version of Apache? |
Server version: Apache/2.4.10 (Debian) Well, to be honest, I didn't know of that module until websockets refused to work on HTTPS. I was using a simple mod_proxy with the ProxyPass and ProxyPassReverse until I had to make ws work through the proxy. However, I disabled it and it made no difference. The problem here is that websockets are not really playing nice the with apache proxy redirection... Or Apache is unable to properly handle those redirections. I am willing to try any other solution or way of having websockets working through HTTPS in my subdomain api.myserver.com. I guess this is a very common scenario, as node instances usually go through PM2 or similar to keep multiple alive instances, and nginx/apache is used as the webserver, thus needing some kind of proxy to tie node+ws to the https web service. |
Yes, I use a similar setup in production. The only difference is that I use NGINX instead of Apache and it works as a SSL terminating reverse proxy. Apache 2.4 is kinda bad as it uses a thread for each WebSocket connection, but this doesn't mean that it shouldn't work. I remember I got it working for a client some time ago. |
If you could have a look sometime and maybe give me an insight of what I'm doing wrong, it would be greatly appreciated 😊 |
@DigitalLeaves this super simple example works on my machine (no SSL)
Config:
The node server: 'use strict';
const WebSocket = require('ws');
const http = require('http');
const fs = require('fs');
const server = http.createServer((req, res) => {
res.setHeader('Content-Type', 'text/html');
fs.createReadStream(__dirname + '/index.html').pipe(res);
});
const wss = new WebSocket.Server({ server });
server.listen(3000, () => console.log('listening on port 3000')); index.html: <!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<p>It works!</p>
<script>
(function () {
var ws = new WebSocket('ws://' + location.host + '/ws');
ws.onopen = function () {
console.log('connected');
};
})();
</script>
</body>
</html> Maybe it's the trailing slash on
|
Hi there! curiously enough, that fixed it 🎉. That extra "/" was really messing around with the Apache proxy configuration. I'm kind of pissed off of the lack of documentation on this existing on the internet. Thank you very much for your support @lpinca, you really saved me from hours of banging my head against a wall. I was even thinking on dropping websockets altogether. Thank you so very much! |
hello guys how are you?? i am having a similar error and i do not know what to do? |
#893 (comment) |
@nitin992503 yes, at the end it was a problem with the IP address, you need to pass the public IP through the path (I have no idea what you are doing), also I have to start apache on the server side |
how about doing this with ssl? |
I solved the configuration problem of apache reverse proxy wss, and now publish the configuration parameters.
|
@DigitalLeaves hy how you solved this problem actually I am facing the same issue. Which extra slash you removed. Could you please help me with that. I am struggling with this issue from last 3 days please help me with this. |
Yes, the extra tail at /wss/ was the problem. |
If you're on Angular it should be:
|
This works perfect |
Hello, first of all, thanks for your awesome library. I was able to setup and get WS running in a breeze. Thanks.
I have a problem with ws, I'm unable to make websockets work on an apache proxy through HTTPS. Websockets are working properly if no (apache) http(s) proxy is used.
My setup: I have an apache server with multiple virtual hosts. I have a HTTPS webpage for myserver.com and the HTTPS API with node/express/ws in api.myserver.com subdomain through the proxy, that redirects the requests to the node.js instance (multiple instances on PM2) running on port 3333.
This is my apache virtual host for the subdomain:
This works OK for redirecting the connections to the node express backend. I have installed mod_proxy, mod_proxy_http and mod_proxy_wstunnel.
This is the node.js API backend: first, I initialize express, sessions, etc.
And then I setup the HTTPS server securely, using the same certificates that in APACHE:
Last, I setup the websocket connections:
Then my clients just try to connect to websockets securely (because, being a HTTPS app, I cannot use the ws:// insecure websockets connection):
And then I get always the same error 302:
If I test on a local server directly to the node instance https://localhost:3333/ it's working perfectly and websockets work as they should.
Any idea of how to solve this? Is there a problem with ws redirections made by Apache proxy modules?
The text was updated successfully, but these errors were encountered: