Nginx Socket.io SSL没有给出io错误 [英] Nginx Socket.io SSL giving io is not defined error

查看:78
本文介绍了Nginx Socket.io SSL没有给出io错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am tring to make client server communication using socket.io on a server with https and reverse proxy as nginx.But on client side its giving following errors :

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

1) GET https://example.com/socket.io/socket.io.js 404 not found

2)参考错误:io未定义

2) Reference Error: io is not defined

这是我的nginx服务器块

This is my nginx server block

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;
        } 

}

这是我的节点服务器文件

This is my node server file

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中,而不是根目录中(但我不认为这会有所不同)

my node application is in directory /usr/share/nginx/html and not in root( but i dont think this can make any difference )

推荐答案

最后,它已修复.我对nginx服务器块进行了更改.

Finally It was fixed. I made changes to nginx server block.

这是我的新Nginx服务器块

This is my new nginx server block

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;
        } 

}

这篇关于Nginx Socket.io SSL没有给出io错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆