簡體   English   中英

Socket.io無法在nginx + node.js + php app中連接

[英]Socket.io can't connect in nginx + node.js + php app

我試圖用PHP app和node.js一起運行nginx(這部分工作正常)。 另外我想將socket.io添加到此設置中,但不幸的是我無法在客戶端和服務器之間建立連接(看起來像連接超時?)。

server.js

var app = require("http"),
    redis = require("redis"),
    io = require('socket.io')(app);


io.sockets.on( 'connection', function( client ) {
    console.log( "New client !" );

    io.sockets.emit('msg', { msg: 'Foo bar' } );
});

app.createServer().listen(3000);
console.log("Server running at 127.0.0.1:3000");

client.js

        <script src="https://cdn.socket.io/socket.io-1.4.5.js"></script>
<!--        <script src="/js/socket.io-client/socket.io.js" type="text/javascript"></script>-->
        <script>
            var socket = io.connect('http://localhost:3000');
            socket.on('msg', function(msg) {
                alert('News from server: ' + msg.msg);
            });
        </script>

(我嘗試過很多url變種。有/無http,127.0.0.1:3000,myappp.dev等)

這是我目前的nginx配置

server {
  listen                *:80;

  server_name           myapp.dev www.myapp.dev;
  client_max_body_size 1m;

  root /var/www/public;
    index  index.html index.htm index.php;

  access_log            /var/log/nginx/nxv_b3ro4tj2wiaf.access.log;
  error_log             /var/log/nginx/nxv_b3ro4tj2wiaf.error.log;
  location / {

    root  /var/www/public;
    try_files $uri $uri/ /index.php$is_args$args;
     autoindex off;
    index  index.html index.htm index.php;

    }


 location ~ \.php$ {

    set $path_info $fastcgi_path_info;
    fastcgi_index index.php;
    fastcgi_split_path_info ^(.+\.php)(/.*)$;
    try_files $uri $uri/ /index.php$is_args$args;
    include /etc/nginx/fastcgi_params;
    fastcgi_pass 127.0.0.1:9000;

    fastcgi_param SCRIPT_FILENAME $request_filename;

  }
  sendfile off;
}

但我也嘗試了一些其他配置,例如:

Node.js + Nginx - 現在怎么辦?
https://www.digitalocean.com/community/questions/running-both-php-and-node-js-on-nginx-on-the-same-server

我跑的時候

DEBUG=socket.io:* nodemon --debug test.js

我明白了

[nodemon] 1.9.2
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `node --debug test.js`
Debugger listening on port 5858
  socket.io:server initializing namespace / +0ms
Server running at 127.0.0.1:3000

沒有錯誤,沒有。
知道我做錯了什么嗎? 基本上我想做這樣的事情https://github.com/jdutheil/nodePHP 但是我無法讓它發揮作用。 唯一的區別是來自github的app正在使用express框架。 我沒有。

我很確定我的服務器端口3000是打開的(順便說一句,它是流浪漢)

編輯:

這是我的nginx配置。

upstream app_zendnode {
    server 127.0.0.1:3000;
    keepalive 8;
}

server {
  listen                *:80;

  server_name           zendnode.dev;
  client_max_body_size 1m;

  root /var/www/public;
    index  index index.html index.htm index.php;

  access_log            /var/log/nginx/zendnode.access.log;
  error_log             /var/log/nginx/zendnode.error.log;

  location / {
     root  /var/www/public;
     try_files $uri $uri/ index.php;
     autoindex on;

      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_set_header X-NginX-Proxy true;

      proxy_pass http://app_zendnode/;
      proxy_redirect off;

      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "upgrade";

  }

 location ~ \.php$ {


    fastcgi_index index.php;
    fastcgi_split_path_info ^(.+\.php)(/.*)$;
    try_files $uri $uri/ index.php /index.php$is_args$args;
    include /etc/nginx/fastcgi_params;
    fastcgi_pass 127.0.0.1:9000;

    fastcgi_param SCRIPT_FILENAME $request_filename;
    fastcgi_param APP_ENV dev;

  }
  sendfile off;
}

Node.js服務器

var socket = require( 'socket.io' );
var express = require( 'express' );
var http = require( 'http' );

var app = express();
var server = app.listen(3000);

var io = socket.listen( server )

io.sockets.on( 'connection', function( client ) {
    console.log( "New client !" );

    io.sockets.emit('msg', { msg: 'Foo bar' } );
});

Node.js客戶端

    var socket = io.connect('127.0.0.1:3000');
    socket.on('msg', function(msg) {
        alert('News from server: ' + msg.msg);
    });
    socket.on('connect', function () {
        socket.emit('hi!');
    });

現在我無法讓PHP工作。 我收到404 HTTP錯誤,空白頁面包含文字:

Cannot GET / 

我在這個問題上非常喜歡Node.js + Nginx - 現在怎么樣?

編輯2

這是我目前的配置。
Nginx的:

upstream app_zendnode {
    server 127.0.0.1:3000;
    keepalive 8;
}

server {
  listen                *:80;

  server_name           zendnode.dev;
  client_max_body_size 1m;

  root /var/www/public;
    index  index index.html index.htm index.php;

  access_log            /var/log/nginx/zendnode.access.log;
  error_log             /var/log/nginx/zendnode.error.log;

  location / {
      root /var/www/public;
      try_files $uri $uri/ index.php;
      autoindex on;
  }

 location /nodejsapi/ {
     root  /var/www/public;
     try_files $uri $uri/ index.php;
     autoindex on;

      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_set_header X-NginX-Proxy true;

      proxy_pass http://app_zendnode/;
      proxy_redirect off;

      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "upgrade";

  }

 location ~ \.php$ {


    fastcgi_index index.php;
    fastcgi_split_path_info ^(.+\.php)(/.*)$;
    try_files $uri $uri/ index.php /index.php$is_args$args;
    include /etc/nginx/fastcgi_params;
    fastcgi_pass 127.0.0.1:9000;

    fastcgi_param SCRIPT_FILENAME $request_filename;
    fastcgi_param APP_ENV dev;

  }
  sendfile off;
}

Node.js(服務器)

var socket = require( 'socket.io' );
var express = require( 'express' );
var http = require( 'http' );

var app = express();
var server = app.listen(3000);

var io = socket.listen( server )
io.use(monitorio({ port: 8000 }));

io.sockets.on( 'connection', function( client ) {
    console.log( "New client !" );

    io.sockets.emit('msg', { msg: 'Foo bar' } );
});

Client.js

    var socket = io.connect('127.0.0.1:3000', {path: '/nodejsapi/'});
    socket.on('msg', function(msg) {
        alert('News from server: ' + msg.msg);
    });
    socket.on('connect', function () {
        socket.emit('hi!');
    });

Nginx未配置為充當nodejs的反向代理。 php和nodejs請求都應該通過nginx上的相同端口(同源策略)。 你錯過了這樣的東西(查看你的教程)

server {
    listen 80;
    server_name example.com;
    location / {
        proxy_pass http://APP_PRIVATE_IP_ADDRESS: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;
    }
}

還有一件事。 要調試這樣的問題,您應該使用telnet和tcpdump。

在服務器上,您可以偵聽傳入的連接

tcpdump -i eth0 'port 80'

在客戶端上,您可以測試和發出請求

telnet mysrv.com 80

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM