socket.io只能在本地使用nginx


socket.io only works locally with nginx

(PHP使用nginx运行,我使用NODEJS中的socket.io)如果我在本地尝试我的网站(使用2个不同的web浏览器),一切都可以。但是,如果我托管我的网站(托管在我家),我仍然可以用其他计算机访问它,但我的app.js的功能没有执行。。。

这是我从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"

在我处理NodeJS函数的页面中:(位于:/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');

我的nodeJs文件app.js:(位于:/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");

这是我的Nginx默认文件(位于:/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;
}
}

谢谢你,我希望我的问题是足够准确的

我终于解决了我的问题,在index.php文件中我修改了

var socket = io.connect('127.0.0.1:3000');

通过

var socket = io.connect('http://'+window.location.host+':3000');

现在一切正常!:)