我打算在服务器上使用socket.io进行客户端服务器通信,使用https和反向代理作为nginx.But在客户端提供以下错误:
1)GET https://example.com/socket.io/socket.io.js 404找不到
2)参考错误:io没有定义
这是我的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>
这是urlhttps://example.com
我的节点应用程序在/ usr / share / nginx / html目录中,而不是在根目录下(但是我不认为这可以使任何区别)
最后它被修复了。 我对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; } }