2017-08-02 55 views
0

我特林使用以https服務器上socket.io使客戶端服務器通信和客戶端反向代理nginx.But一側的捐贈以下錯誤:Nginx的Socket.io SSL給IO是沒有定義的錯誤

1)GET https://example.com/socket.io/socket.io.js 404未找到

2)參考錯誤:IO沒有定義

這是我的nginx服務器塊

server { 
     listen 80 default_server; 
     listen [::]:80 default_server ipv6only=on; 


     root /usr/share/nginx/html; 
     index index.html index.htm; 

     # Make site accessible from http://localhost/ 
     server_name example.com www.example.com; 

     location/{ 
     proxy_pass http://127.0.0.1:8080; 
     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; 

     try_files $uri $uri/ =404; 

     } 

     listen 443 ssl; 
     ssl_certificate /etc/letsencrypt/live/myreactnative.com/fullchain.pem; 
     ssl_certificate_key /etc/letsencrypt/live/myreactnative.com/privkey.pem; 
     include /etc/letsencrypt/options-ssl-nginx.conf; 
     if ($scheme != "https") { 
      return 301 https://$host$request_uri; 
     } 

} 

這是我的節點服務器文件

var express = require('express'); 
var app = express(); 
var serverPort = 8080; 
var http = require('http'); 
var server = http.createServer(app); 
var io = require('socket.io')(server); 

app.get('/', function(req, res){ 
    console.log('get /'); 
    res.sendFile(__dirname + '/index.html'); 
}); 
server.listen(serverPort, function(){ 

    console.log('server up and running at %s port', serverPort); 
}); 

io.on('connection', function(socket){ 
    console.log('connection'); 
    socket.on('disconnect', function(){ 
    console.log('disconnect'); 
    }) 
}) 

我的客戶端是這樣的:

<html> 
<head> 
    <title>socket client</title> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> 
</head> 
<body> 
</body> 
<script src="/socket.io/socket.io.js"></script> 
<script type="text/javascript"> 
var socket = io(); 
</script> 
</html> 

這是網站的URL https://example.com

我節點應用是在目錄/ usr /共享/ nginx的/ HTML而不是根(但我不認爲這可以有任何區別)

+0

點1.簡單地告訴你有/socket.io/socket.io.js網址在任何socket.io.js文件。第二個問題可能是第1點的後果。請驗證您的socket.io客戶端庫的位置,並將其正確地引用到您的HTML代碼中。這兩個錯誤應該由此來解決。 – user1970395

+0

我明白了。嗯,很難說,我想你是從節點服務你的socket.io.js文件然後..我只使用節點的WS(socket.io)通信和服務器從我的PHP應用程序的一切。我的猜測是查看proxy_set_header調用是否不會打破它(刪除它們 - 您將不會連接到WS,但會看到是否提供了JS文件)。然後try_files指令可能會給你404錯誤。 – user1970395

回答

0

最後它被修復了。我對nginx服務器塊進行了更改。

這是我的新的nginx服務器塊

server { 
     listen 80; 

     root /usr/share/nginx/html; 
     index index.html index.htm; 

     # Make site accessible from http://localhost/ 
     server_name example.com www.example.com; 

     location/{ 
     proxy_pass http://127.0.0.1:8080; 
     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; 

     } 

     listen 443 ssl; 
     ssl_certificate /etc/letsencrypt/live/myreactnative.com/fullchain.pem; 
     ssl_certificate_key /etc/letsencrypt/live/myreactnative.com/privkey.pem; 
     include /etc/letsencrypt/options-ssl-nginx.conf; 
     if ($scheme != "https") { 
      return 301 https://$host$request_uri; 
     } 

}