mardi 14 juin 2016

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

I'm trying to run nginx with PHP app and node.js together (this part works fine). Additionaly I would like to add socket.io to this setup, but unfortunatelly I can't establish connections between client and server (looks like connection time out?).

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>

(I have tried many variant of url. With/without http, 127.0.0.1:3000, myappp.dev etc.)

Here is my current nginx configuration

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;
}

But I also tried few other configurations for example:

Node.js + Nginx - What now?
https://www.digitalocean.com/community/questions/running-both-php-and-node-js-on-nginx-on-the-same-server

When I run

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

I get

[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

No errors, nothing.
Any idea what am I doing wrong? Basically I would like to do something like this https://github.com/jdutheil/nodePHP. But I cant get it working. The only difference is that app from github is using express framework. Mine not.

I'm pretty sure port 3000 is open on my server (it's vagrant by the way)

EDIT:

here is my nginx configuration.

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 server

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 client

    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!');
    });

Now I can't get even PHP working. I get 404 HTTP error, blank page with text:

Cannot GET / 

I did excatly like in this question Node.js + Nginx - What now?

Aucun commentaire:

Enregistrer un commentaire