繁体   English   中英

NodeJS和Nginx

[英]NodeJS and Nginx

在过去的两天里,我一直在寻找解决方案,无法解决。 我有一个使用套接字io的节点应用程序生活在一个子域下,该子域位于几个文件夹下https://my.example.com/node/app1/每次访问URL时,我都会获取GET https://my.example.com/socket.io/socket.io.js Uncaught ReferenceError: io is not defined

我的文件如下所示: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>

我猜想您会在前端收到该错误,可能浏览器正在http://localhost/socket.io/socket.io.js而不是http://localhost/node/app1/socket.io/socket.io.js寻找socket.io http://localhost/node/app1/socket.io/socket.io.js

确保使用相对导入:

<script src="socket.io/socket.io.js"></script>

您可能希望保留<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;
}

因此,我终于根据你们的建议弄清楚了这是我想出的:请注意域: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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM