简体   繁体   中英

apache ssl (443) node.js ssl (8081). Client can't find socket.io.js

I have spent two days on this. Hoping someone has the answer.

I have an apache server running SSL on port 443. I have a node.js server running SSL on port 8081.

In the client, I am trying to include the socket.io/socket.io.js file as follows:

<script src="https://my_url.com:8081/socket.io/socket.io.js"></script>

I don't get a 404 (not found). In the Chrome javascript console, I get Status = (failed) and Type = undefined. I have tried suggestions I've found (using src="localhost.." etc, but they don't work either).

It works for my http version. I include the file like this:

<script src="http://my_url.com:8080/socket.io/socket.io.js"></script>

Not sure why the SSL version is not working. Any ideas? Thanks in advance!!

from your comment i guess you have a CORS problem:

http://en.wikipedia.org/wiki/Cross-origin_resource_sharing

If you change protocoll the origin is not the same following the same origin policy. You have to include a header for this.

https://developer.mozilla.org/en-US/docs/JavaScript/Same_origin_policy_for_JavaScript?redirectlocale=en-US&redirectslug=Same_origin_policy_for_JavaScript

The warning about insecure content is a hint what caused the problem: You are trying to access an origin via script which is not the origin of the script. This is a security issue as it may provide a leak allowing harmful scripts to be injected into the page

DO NOT SET THE ProxyPass to Localhost! That's not working!

Do this in your Apache configuraton:

<VirtualHost *:443>
    ServerName domain.com
    SSLProxyEngine On
    RequestHeader set Front-End-Https "On"
    ProxyPass / https://domain.com:3000/ retry=1 acquire=3000 timeout=600 Keepalive=On
    ProxyPassReverse / https://domain.com:3000/

    SSLEngine on
    SSLCertificateKeyFile    /etc/ssl/private/domain.com.key
    SSLCertificateFile       /etc/ssl/certs/domain.com.crt
    SSLCertificateChainFile  /etc/ssl/certs/chaincert.crt
</VirtualHost>

Now, Nodejs can also have SSL:

var express = require('express');
var connect = require('connect');
var app = express();
var fs = require('fs');

var ssl_options = {
    key: fs.readFileSync('/etc/ssl/private/domain.com.key'),
    cert: fs.readFileSync('/etc/ssl/certs/domain.com.crt'),
    ca: fs.readFileSync('/etc/ssl/certs/domain.com.ca')
};

var server = require('https').createServer(ssl_options, app);
var port = process.env.PORT || 3000;

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

server.listen(port, function(err) {
    console.log(colors.green('https server running on port ' + port));
});

And in your client do this:

var socket = io.connect('https://domain.com:3000', { path: '/socket.io', 'flash policy port': 3000 });

for me, this is working fine!

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