5

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?

EDIT 2

Here is my current configuration.
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 (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.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!');
    });
Community
  • 1
  • 1
user1409508
  • 623
  • 1
  • 12
  • 38

1 Answers1

3

Nginx is not configured to act as a reverse proxy for nodejs. Both php and nodejs requests should go through same port (Same origin policy) on nginx. You're missing something like this (check in your tutorial)

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

One more thing. To debug issues like this, you should use telnet and tcpdump.

On server, you listen to incoming connections

tcpdump -i eth0 'port 80'

On client, you test and issue requests with

telnet mysrv.com 80
Mitja Gustin
  • 1,723
  • 13
  • 17
  • I don't understand. You say that PHP and NodeJs should go through same port. But I can't set node.js to listen on port 80 because it's already taken. So what do you mean by 'requests should go through same port'? – user1409508 Jun 10 '16 at 14:39
  • It means nginx should act as a proxy. Proxying specific url pattern on port 80 to another url on port 3000. It's called reverse proxy. For the client, this appears to be on same port. – Mitja Gustin Jun 10 '16 at 20:43
  • You must bind proxy to specific url not to "/". That's why even php is not working. Lets say: location /nodejsapi/ { ... – Mitja Gustin Jun 14 '16 at 11:14
  • I need PHP and Node.js to work parallel, not only for specific url. Like in this example https://github.com/jdutheil/nodePHP – user1409508 Jun 14 '16 at 11:37
  • but this example is only design time picture. In runtime, the things can be structured like this, or differently. There is no need for node script to be under same js folder. You will have to differentiate with URL (all node requests go through /nodejsapi/ folder, all *.php goes through url *.php ending. There is absolutely no problem with this. The main requirement here is Same Origin policy. Ports must be the same, protocol and server address. – Mitja Gustin Jun 14 '16 at 11:52
  • Only after first / things can differentiate. Same origin policy describes relations between url and ajax request. You should not make ajax request (or websocket) to another port / protocol or server. You you can make it to different url, if this things are equal. – Mitja Gustin Jun 14 '16 at 11:52
  • Ok, let's say I understand (these things - nginx, socket.io is still new for me). I changed `location / {` to `location /nodejsapi/ {` like you said. I also changed node client to var `socket = io.connect('127.0.0.1:3000', {path: '/nodejsapi/'});` but it still doesn't work. I mean PHP is working now, but socket client can't connect to node.js server. Did I miss something? – user1409508 Jun 15 '16 at 08:17
  • let me install your project and post the complete solution. You will hear from me soon. I'm sure it must be socket = io.connect('127.0.0.1:80', {path: '/nodejsapi/'}). Nginx will redirect to port 3000 (via proxy config). – Mitja Gustin Jun 15 '16 at 09:09
  • Ok. I edited my main post and added my lastest configuration – user1409508 Jun 15 '16 at 09:38
  • Hey, did you try to install my project? – user1409508 Jun 21 '16 at 07:51