I am trying to make a connection to a client server using socket.io on a server with https and a reverse proxy like nginx. But on the client side, it gives the following errors:
1) GET https://example.com/socket.io/socket.io.js 404 not found
2) Original error: io not defined
This is my nginx server server
server {
listen 80 default_server;
listen [::]:80 default_server ipv6only=on;
root /usr/share/nginx/html;
index index.html index.htm;
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 as follows:
<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 URL of the website https://example.com
my node application is located in the / usr / share / nginx / html directory, and not in the root directory (but I donβt think it might matter)
source
share