socket.io只能在本地使用nginx

时间:2016-05-03 12:48:42

标签: php node.js nginx websocket socket.io

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

这是我在nginx上的最后一个error.log:

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;
}
}

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

1 个答案:

答案 0 :(得分:1)

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

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

通过

var socket = io.connect('http://'+window.location.host+':3000');
现在一切正常! :)