gZ Fab
gZ Fab

Reputation: 91

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

Upvotes: 0

Views: 878

Answers (1)

gZ Fab
gZ Fab

Reputation: 91

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 ! :)

Upvotes: 1

Related Questions