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!');
    });
2
  • from which directory you socket server is running? Commented May 23, 2016 at 9:34
  • Try using telnet to connect to your 3000 port - both from inside and from outside vagrant virtual machine. Commented May 31, 2016 at 10:34

1 Answer 1

3
+50

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
Sign up to request clarification or add additional context in comments.

10 Comments

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'?
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.
You must bind proxy to specific url not to "/". That's why even php is not working. Lets say: location /nodejsapi/ { ...
I need PHP and Node.js to work parallel, not only for specific url. Like in this example github.com/jdutheil/nodePHP
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.