【问题标题】:Nginx Socket.io SSL giving io is not defined errorNginx Socket.io SSL给io未定义错误
【发布时间】:2017-08-02 17:16:20
【问题描述】:

我正在尝试在使用 https 和反向代理作为 nginx 的服务器上使用 socket.io 进行客户端服务器通信。但在客户端它给出以下错误:

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

2) 引用错误:io is not defined

这是我的 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>

这是网址https://example.com

我的节点应用程序在目录 /usr/share/nginx/html 中,而不是在根目录中(但我认为这不会有任何区别)

【问题讨论】:

  • Point 1. 简单地告诉你 /socket.io/socket.io.js URL 下没有 socket.io.js 文件。第二个问题可能是第 1 点的结果。请验证您的 socket.io 客户端库的位置,并在您的 HTML 代码中正确引用它。这两个错误都应该被修复。
  • 我明白了。嗯,很难说,我想你是从 node 提供你的 socket.io.js 文件。我只使用 node 进行 WS (socket.io) 通信,并从我的 PHP 应用程序中服务器所有内容。我的猜测是看看 proxy_set_header 调用是否不会破坏它(删除它们 - 你不会连接到 WS,但会查看是否提供了 JS 文件)。那么 try_files 指令也可能是给你 404 错误的原因。
  • 试试这个解决方案stackoverflow.com/a/62187296/268598

标签: node.js ssl nginx socket.io reverse-proxy


【解决方案1】:

终于解决了。我对 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;
        } 

}

【讨论】:

猜你喜欢
  • 2011-08-03
  • 2022-12-11
  • 1970-01-01
  • 2012-03-23
  • 2020-04-14
  • 2014-09-14
  • 2014-08-06
  • 2011-11-28
  • 1970-01-01
相关资源
最近更新 更多