gpt4 book ai didi

node.js - Nginx Socket.io SSL 给 io is not defined 错误

转载 作者:太空宇宙 更新时间:2023-11-03 14:38:30 26 4
gpt4 key购买 nike

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

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

2)引用错误:io未定义

这是我的 nginx 服务器 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;
}

}

这是我的 Node 服务器文件

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

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

最佳答案

终于修好了。我对 nginx 服务器 block 进行了更改。

这是我的新 nginx 服务器 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;
}

}

关于node.js - Nginx Socket.io SSL 给 io is not defined 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45467169/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com