I am trying to run nginx with a PHP application and node.js together (this part works fine). Also, I would like to add socket.io to this setting, but unfortunately I canβt establish a connection between the client and server (does it look like the connection time?).
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> var socket = io.connect('http://localhost:3000'); socket.on('msg', function(msg) { alert('News from server: ' + msg.msg); }); </script>
(I tried a lot of url options. With / without http, 127.0.0.1{000, 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 several 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 started
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 mistakes, nothing.
Any idea what I'm doing wrong? Basically, I would like to do something like https://github.com/jdutheil/nodePHP . But I canβt make it work. The only difference is that the application from github uses the express structure. My not.
I am sure that port 3000 is open on my server (by the way, it is wandering around)
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 not get PHP to work. I get an HTTP 404 error, a blank page with text:
Cannot GET /
I really liked Node.js + Nginx in this question - 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!'); });