Nginx Socket.io SSL giving io is not defined error

2020-05-03 12:28发布

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 not found

2) Reference Error: io is not defined

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

my client side is like this :

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

This is the website url https://example.com

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

1条回答
▲ chillily
2楼-- · 2020-05-03 12:38

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

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

}
查看更多
登录 后发表回答