这是我的问题,
背景:
我有一台NGinx服务器正在运行。 NGinx使用证书启用HTTPS。没问题。
我有一个NodeJS服务器工作,它应该使用socketIO来处理websockets。
我正在测试Mozilla Firefox(这可能不是最好的......)
应用程序首先在HTTP上,一切正常。现在所有人都切换到HTTPS,我面对的是一个"交叉起源"问题。
我已经尝试了几种解决方案,但它们都没有为我工作......我将解释我在帖子末尾尝试的内容。
首先,让我向您展示有关的文件,nodeJS的server.js,NGinx服务器的nginx.conf以及"客户端脚本"应该连接websocket。
server.js - NodeJS
var app = require('express')();
var fs = require('fs');
var https = require('https');
var _ = require('underscore');
var socketio = require('socket.io');
var options = {
key: fs.readFileSync('/ssl/file.key'),
cert: fs.readFileSync('/ssl/file.crt')
};
var server = https.createServer(options).listen(9001,function(){
console.log("SERVER LISTENING");
});
io = socketio.listen(server,{origins:'*:*'});
var connectedSockets = {};
_.each(io.nsps, function(nsp){
nsp.on('connect', function(socket){
if (!socket.auth) {
delete nsp.connected[socket.id];
}
});
});
function checkAuthToken(token,callback){
callback(null,true);
}
io.on('connection',function(socket){
socket.broadcast.emit('hi');
socket.on('disconnect',function(){
console.log('a user disconnected');
});
socket.on('chat message',function(msg){
io.emit('chat message',msg);
});
socket.on('authenticate', function(data){
checkAuthToken(data.token, function(err, success){
if (!err && success){
socket.auth = true;
_.each(io.nsps, function(nsp) {
if(_.findWhere(nsp.sockets, {id: socket.id})) {
nsp.connected[socket.id] = socket;
}
});
}
});
});
});
这是一个简单的"聊天应用程序,需要完成几件事情,比如应该检查身份验证的脚本......
nginx.conf - NGinx服务器配置
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
map $http_upgrade $connection_upgragde {
default upgrade;
'' close;
}
sendfile on;
server {
listen 80;
server_name 192.168.100.22;
rewrite ^ https://$http_host$request_uri? permanent;
}
server {
listen 443;
ssl on;
ssl_certificate /ssl/file.crt;
ssl_certificate_key /ssl/file.key;
server_name 192.168.100.22;
root E:/www;
index index.php index.html index.htm;
add_header Access-Control-Allow-Origin *;
add_header Strict-Transport-Security "max-age=31536000; includeSubdomains";
proxy_set_header X-Forwarded-For $remote_addr;
server_tokens off;
charset utf-8;
error_page 500 502 503 504 /50x.html;
location ~ \.php$ {
try_files $uri =404;
include /Nginx/nginx-1.8.0/conf/fastcgi_params;
fastcgi_param HTTPS on;
fastcgi_param HTTP_SCHEME https;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
}
Client.js - index.php中的连接脚本
<script src="./jquery-1.11.1.js"></script>
<script src="./socket.io-1.3.5.js"></script>
<script>
var socket = io('wss://192.168.100.22:9001');
$("form").submit(function(){
socket.emit('chat message',$('#m').val());
$('#m').val('');
return false;
});
socket.on('connect', function(){
socket.emit('authenticate', {token: 456456456,rights:'any'});
});
socket.on('chat message', function(msg){
$('#messages').append($('<li>').text(msg));
});
socket.on('hi',function(){
$('#messages').append($('<li>').text('connection'));
});
</script>
服务器位于192.168.100.22 IP
Nginx听80&amp; 443并将所有流量重定向到443(https)
通过nginx访问文件夹是可以的,当前的聊天测试位于192.168.100.22/websocks/index.php
错误消息如下:
GET https://192.168.100.22:9001/socket.io/?EIO=3&transport=polling&t=1432131062725-0 Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://192.168.100.22:9001/socket.io/?EIO=3&transport=polling&t=1432131062725-0. Reason: CORS request failed
实际上,我已经尝试将其添加到nodejs脚本中:
https.globalAgent.options.rejectUnauthorized = false;
app.get('*',function(req,res,next){
res.header("Access-Control-Allow-Origin","*");
res.header("Access-Control-Allow-Headers","X-Requested-With");
next();
});
但是还没有工作......
感谢您阅读/帮助
答案 0 :(得分:1)
验证您的SSL证书是否有效且名称是否与您的域匹配。看起来您使用的是IP地址而不是证书的域名。当Firefox抱怨证书时,我看到了CORS错误。