我以前有一個Socket.io腳本可以通過http運行,但是升級到https已經破壞了它。我已經在服務器上安裝了證書,但沒有運氣。爲服務器設置的代碼是:通過SSL使用Socket.io Node.js的錯誤網關錯誤
var https = require('https'),
fs = require('fs');
var options = {
key: fs.readFileSync('/etc/nginx/ssl/default/54082/server.key'),
cert: fs.readFileSync('/etc/nginx/ssl/default/54082/server.crt')
};
var app = https.createServer(options);
var io = require('socket.io').listen(app);
但是在Web瀏覽器的頁面無法連接到它,並在控制檯顯示the server responded with a status of 502 (Bad Gateway)
響應。
關於腳本設置是否錯誤的任何想法?或者可能是Nginx設置中的一些東西?
非常感謝
編輯:我使用連接前端代碼:
<script type="text/javascript" src="https://socket.example.com/socket.io/socket.io.js"></script>
<script>
var io = io('https://socket.example.com', { secure: true });
</script>
編輯:: Nginx的配置:
# FORGE CONFIG (DOT NOT REMOVE!)
include forge-conf/socket.example.co.uk/before/*;
server {
listen 443 ssl;
server_name socket.example.co.uk;
root /home/forge/socket.example.co.uk;
# FORGE CONFIG (DOT NOT REMOVE!)
include forge-conf/socket.example.co.uk/server/*;
location/{
proxy_pass https://socket.example.co.uk:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
# FORGE CONFIG (DOT NOT REMOVE!)
include forge-conf/socket.example.co.uk/after/*;
你路過{安全:真正}在連接到服務器? http://stackoverflow.com/questions/6599470/node-js-socket-io-with-ssl –
沒有必要通過nginx代理node.js。港口是開放的,所以與'io連接。connect(':',{secure:true});從客戶端獲取和運行所需的全部內容。不可能通過nginx進行代理(並且因爲節點綁定到所有接口,所以不必這樣做,並且由於DNS配置,域仍會路由到您的ip:端口)。我從字面上將我的socket io升級到了昨天沒有問題的https。 –
r3wt
@ r3wt所以你說的域名應該是不同的?它應該是什麼?我使用代碼'var io = io('https://socket.example.com/socketio/socketio.js',{secure:true});' – samiles