NodeJS 和 Nginx
2016-09-18
637
过去两天我一直在寻找解决方案,但一直没找到。我有一个使用套接字 io 的节点应用程序,它位于子域下,该子域位于几个文件夹下
https://my.example.com/node/app1/
每次我访问 URL 时,我都会得到
GET https://my.example.com/socket.io/socket.io.js
Uncaught ReferenceError:io 未定义
这是我的文件的样子: nginx 配置文件:
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 /node/app1/ {
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;
}
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;
}
access_log /var/virdisoft/logs/access.log;
error_log /var/virdisoft/logs/error.log;
}
app.js
var app = require('http').createServer(handler)
var io = require('socket.io')(app);
var fs = require('fs');
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('http://localhost:3131');
socket.on('news', function (data) {
console.log(data);
socket.emit('my other event', { my: 'data' });
});
</script>
3个回答
我猜你是在前端收到这个错误,可能是浏览器正在
http://localhost/socket.io/socket.io.js
中寻找
socket.io
,而不是
http://localhost/node/app1/socket.io/socket.io.js
确保使用相对导入:
<script src="socket.io/socket.io.js"></script>
martriay
2016-09-18
您可能希望保留剩余的
<script src="/socket.io/socket.io.js"></script>
并为
socket.io
添加另一个
location
指令。您可以在 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;
}
Arif Khan
2016-09-18
所以我终于弄明白了,使用你们的建议,这就是我想出的:请注意域名: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>
Sonu
2016-09-19