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

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

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

Related

502 bad gateway when access nginx from a docker container

I am trying to create a php inside my docker container
However, when I go to the localhost on my browser, I keep getting the 502 bad gateway.
The error log shows
[error] 11#0: *1 connect() failed (111: Connection refused) while connecting to upstream, client: (my IP), server: localhost, request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "localhost"
My nginx conf looks like this
server {
listen 80;
server_name jenkins.local;
root /var/www/html;
index index.php;
access_log /var/log/nginx/localhost-access.log;
error_log /var/log/nginx/localhost-error.log;
location / {
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
try_files $uri =404;
include /etc/nginx/fastcgi_params;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_script_name;
fastcgi_intercept_errors on;
}
}
I am new to docker and PHP so please let me know if I need to provide any other information.
Did you start php-fpm?
Is port 9000 open?
Brief edition
upstream websocketserver {
server localhost:8080;
}
server {
listen 80;
server_name jenkins.local; //Or try localhost;
root /var/www/html;
index index.php;
access_log /var/log/nginx/localhost-access.log;
error_log /var/log/nginx/localhost-error.log;
location / {
proxy_pass http://websocketserver;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
proxy_read_timeout 86400; # neccessary to avoid websocket timeout disconnect
proxy_send_timeout 900s;
proxy_redirect off;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php7.4-fpm.sock; //Check if your php is 7.4, run php -v
}
}

Nginx proxy PHP-FPM + Lighttpd 405 not allowed symfony

Hello I am trying to serve a php web application using Nginx PHP-FPM using a tcp socket on a remote host and a lighttd to serve static content on a remote server.
I succeed to link those three blocks but I have an issue to manage the interactions.
Here my Nginx proxy configuration
upstream xxxx-staging {
server xxxxx.com:81 fail_timeout=0;
}
server {
listen 80;
server_name xxxxxxx.com;
return 301 https://xxxxx.com$request_uri;
}
server {
listen 443 ssl;
server_name xxxxxx.com;
root /xxxxxx/public;
ssl_certificate /etc/nginx/ssl/xxx.com.pem;
ssl_certificate_key /etc/nginx/ssl/xxx.com.key;
access_log /var/log/nginx/xxx.access.log;
error_log /var/log/nginx/xxx.error.log error;
client_max_body_size 256m;
proxy_intercept_errors on;
error_page 404 = /index.php;
error_page 405 = 200$uri;
location = / {index index.php;}
location / {
proxy_pass http://xxxxx-staging;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
auth_basic "Please authenticate";
auth_basic_user_file /etc/nginx/passwords/xxxxxxx.com.passwdfile;
}
location ~ ^/index\.php$(/|$) {
fastcgi_pass xxxxx.com:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors on;
}
}
When I removed error_page 405 = 200$uri; symfony is returning me a method post not allowed and when I put it nginx is returning me a 405.
I clearly missunderstand how the communication are established but don't know where I am wrong.
You are using auth_basic and auth_basic_user_file in the location / block. Am I right that you don't get a prompt to enter a password when you navigate to the side and get instead directly the 405 error?
I interpret the error to mean that an authentication is expected but doesn't happen. I would suggest to move auth_basic and auth_basic_user_file up one level in the server block. That way everything is covered and not only the one location block and you should get the password prompt.
upstream xxxx-staging {
server xxxxx.com:81 fail_timeout=0;
}
server {
listen 80;
server_name xxxxxxx.com;
return 301 https://xxxxx.com$request_uri;
}
server {
listen 443 ssl;
server_name xxxxxx.com;
root /xxxxxx/public;
auth_basic "Please authenticate";
auth_basic_user_file /etc/nginx/passwords/xxxxxxx.com.passwdfile;
ssl_certificate /etc/nginx/ssl/xxx.com.pem;
ssl_certificate_key /etc/nginx/ssl/xxx.com.key;
access_log /var/log/nginx/xxx.access.log;
error_log /var/log/nginx/xxx.error.log error;
client_max_body_size 256m;
proxy_intercept_errors on;
error_page 404 = /index.php;
error_page 405 = 200$uri;
location = / {index index.php;}
location / {
proxy_pass http://xxxxx-staging;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
location ~ ^/index\.php$(/|$) {
fastcgi_pass xxxxx.com:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_intercept_errors on;
}
}
I hope that helps you.

Nginx multiple listeners (with php application)

I want Nginx to serve both my jenkins server and my Laravel (php) application. Here is my Nginx configuration:
server {
listen 80 default_server;
server_name localhost;
location / {
proxy_set_header Host $host:$server_port;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
# Fix the "It appears that your reverse proxy set up is broken" error.
proxy_pass http://127.0.0.1:8081;
proxy_read_timeout 90;
proxy_redirect http://127.0.0.1:8081 https://jenkins.domain.tld;
}
}
server {
listen 9000;
listen [::]:9000;
root /var/www/my-app/public;
index index.php index.html index.htm;
server_name my-app.io;
location / {
try_files $uri $uri/ =404;
}
error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
I am using virtual-box as my virtual machine emulator for ubuntu 14. I am able to access jenkins application by going to http://127.0.0.1:8081 (I have changed the jenkins default port).
But when I am trying to access http://127.0.0.1:9000 I am getting an error, I have checked Ngnix logs but couldn't find any information regarding a request to port 9000.
Also I have made port forwarding in virtual-box, here is a screenshot:
Any help would be great.
Use server_name localhost too for php.

socket.io only works locally with nginx

(PHP runs with nginx, and I use socket.io from NODEJS)
If I try my website locally (using 2 web diffenrets web browzers), everything works.
But If I host my website (hosted in my house), I still can access to it with an other computer, but the functions of my app.js are not executed...
Here is my last error.log from nginx :
2016/05/03 14:11:00 [error] 25016#25016: *108 FastCGI sent in stderr: "PHP message: PHP Notice: Only variables should be passed by reference in /var/www/html/outer_treatment/game/add_message_discussion.php on line 55" while reading response header from upstream, client: 192.168.1.16, server: default, request: "POST /outer_treatment/game/add_message_discussion.php HTTP/1.1", upstream: "fastcgi://unix:/run/php/php7.0-fpm.sock:", host: "192.168.1.13", referrer: "http://192.168.1.13/game.php"
in my page where the NodeJS functions are handled: (located in : /view/game/index.php)
# into the <head>
<script src="/NODEJS/socket.io/socket.io.js"></script>
# into the <body>
var socket = io.connect('127.0.0.1:3000');
And my nodeJs file app.js : (located in : /NODEJS/app.js)
var app = require('express')(),
server = require('http').createServer(app),
io = require('socket.io').listen(server),
fs = require('fs');
io.sockets.on('connection', function(socket)
{
// here my functions
});
server.listen(3000, "127.0.0.1");
Here is my default file of Nginx (located in : /etc/nginx/sites-available)
# the IP(s) on which node server is running.
upstream app_default {
server 127.0.0.1:3000;
keepalive 8;
}
server {
# Default listen lines :
#listen 80 default_server;
#listen [::]:80 default_server ipv6only=on;
# NODE JS listen
#listen 0.0.0.0:80;
listen 80;
#root /usr/share/nginx/html;
root /var/www/html;
index index.php index.html index.htm;
#server_name localhost;
server_name default;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.php;
# Uncomment to enable naxsi on this location
# include /etc/nginx/naxsi.rules;
#NODEJS configuration :
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://127.0.0.1:3000/;
proxy_redirect off;
# the websockets :
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
location ~ \.php$ {
try_files $uri /index.php =404;
fastcgi_pass unix:/run/php/php7.0-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
location ~ \.css {
add_header Content-Type text/css;
}
location ~ \.js {
add_header Content-Type application/x-javascript;
}
location ~ \.png$ {
try_files $uri $uri /$1;
}
}
Thanks you, I hope my problem is enough accurate
I finally resolved my problem, in the index.php file I ve modified
var socket = io.connect('127.0.0.1:3000');
by
var socket = io.connect('http://'+window.location.host+':3000');
everything works now ! :)

How to configure both php and nodejs app together using nginx on the same server

I have an Ubuntu VPC. I have a nodejs app running on it in some folder. I have a php app running on apache in var/www. What I want is to configure nginx so that when a user comes on mydomain.com , he/she is redirected to my node app running on port 3000 and when the user visits mydomain.com/docs , he/she is redirected to my php app running on apache on port 80.
server {
listen 80;
root /var/www;
location / {
try_files $uri $uri/ /index.html;
}
location ~\.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
}
location /doc/ {
alias /usr/share/doc/;
autoindex on;
allow 127.0.0.1;
deny all;
}
location ~/\.ht {
deny all;
}
}
My conf file for nodejs app is:
upstream app_somedomain {
server 127.0.0.1:3000;
}
server {
listen 0.0.0.0:80;
server_name mydomain.com mydomain;
root /some/path;
index index.html index.htm index.php index.js;
location / {
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_somedomain/;
proxy_redirect off;
}
}
I am using nginx for the first time. I am still unable to understand how to achieve this. As in how to serve my nodejs app running on port 3000 to my domain name and mydomain/docs to my php app.
Thanks.

Categories