过去两天我一直在寻找解决方案,但无法弄明白。我有一个节点应用程序正在使用套接字io生活在一个子域下,该子域位于几个文件夹$HOME/
每次我转到URL我得到
Tests
这是我的文件的样子: nginx配置文件:
{
"name": "Debug xunit tests",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "/usr/local/share/dotnet/dotnet",
"args": [
"exec",
"--runtimeconfig",
"${workspaceRoot}/AppNameHere/bin/Debug/netcoreapp1.0/AppNameHere.runtimeconfig.json",
"--depsfile",
"${workspaceRoot}/AppNameHere/bin/Debug/netcoreapp1.0/AppNameHere.deps.json",
"--additionalprobingpath",
"/Users/jdoe/.nuget/packages",
"/Users/jdoe/.nuget/packages/dotnet-test-xunit/1.0.0-rc2-build10015/lib/netcoreapp1.0/dotnet-test-xunit.dll",
"${workspaceRoot}/AppNameHere/bin/Debug/netcoreapp1.0/AppNameHere.dll",
"-namespace",
"Tests"
],
"cwd": "${workspaceRoot}",
"stopAtEntry": false
}
app.js
https://my.example.com/node/app1/
的index.html
GET https://my.example.com/socket.io/socket.io.js
Uncaught ReferenceError: io is not defined
答案 0 :(得分:0)
我猜您在前端收到了该错误,可能浏览器正在socket.io
而不是http://localhost/socket.io/socket.io.js
中寻找http://localhost/node/app1/socket.io/socket.io.js
确保使用相对导入:
<script src="socket.io/socket.io.js"></script>
答案 1 :(得分:0)
您可能希望保持<script src="/socket.io/socket.io.js"></script>
并为location
添加另一个socket.io
指令。您可以在nginx配置文件中显示location /node/app1/ {
location /socket.io {
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_http_version 1.1;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_pass http://localhost:3131;
}
答案 2 :(得分:0)
所以我终于明白了,使用你的家伙建议这就是我提出的:请注意域名:example.com是占位符,所以将exmaple.com和my.example.com更改为你的域名名字是。
NGINX配置:
upstream my_nodejs {
server 127.0.0.1:3131;
}
server {
listen 443 ssl http2;
server_name my.example.com;
include /etc/nginx/include.d/ssl-common;
include /etc/nginx/include.d/ssl-example;
include /etc/nginx/include.d/all-common;
root /var/example/my;
location ~ \.php$ {
fastcgi_pass unix:/var/run/ssl_example_com.sock;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors on;
}
location ^~ /node/app1/ {
try_files $uri @my_nodejs;
}
location ^~ /socket.io/ {
try_files $uri @my_nodejs;
}
location @my_nodejs {
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_http_version 1.1;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_pass http://my_nodejs;
}
access_log /var/example/logs/access.log;
error_log /var/example/logs/error.log;
}
app.js
var fs = require('fs');
var app = require('http').createServer(handler)
var io = require('socket.io')(app);
app.listen(3131, function() {
console.log('Listening on 3131');
});
function handler (req, res) {
fs.readFile(__dirname + '/index.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
});
}
io.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
socket.on('my other event', function (data) {
console.log(data);
});
});
的index.html
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io(); // Removed the path cause it's not needed.
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
</script>