简体   繁体   English

Socket.io无法在nginx + node.js + php app中连接

[英]Socket.io can't connect in nginx + node.js + php app

I'm trying to run nginx with PHP app and node.js together (this part works fine). 我试图用PHP app和node.js一起运行nginx(这部分工作正常)。 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?). 另外我想将socket.io添加到此设置中,但不幸的是我无法在客户端和服务器之间建立连接(看起来像连接超时?)。

server.js 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 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.) (我尝试过很多url变种。有/无http,127.0.0.1:3000,myappp.dev等)

Here is my current nginx configuration 这是我目前的nginx配置

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? Node.js + Nginx - 现在怎么办?
https://www.digitalocean.com/community/questions/running-both-php-and-node-js-on-nginx-on-the-same-server 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 . 基本上我想做这样的事情https://github.com/jdutheil/nodePHP But I cant get it working. 但是我无法让它发挥作用。 The only difference is that app from github is using express framework. 唯一的区别是来自github的app正在使用express框架。 Mine not. 我没有。

I'm pretty sure port 3000 is open on my server (it's vagrant by the way) 我很确定我的服务器端口3000是打开的(顺便说一句,它是流浪汉)

EDIT: 编辑:

here is my nginx 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;

      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 Node.js服务器

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 Node.js客户端

    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. 现在我无法让PHP工作。 I get 404 HTTP error, blank page with text: 我收到404 HTTP错误,空白页面包含文字:

Cannot GET / 

I did excatly like in this question Node.js + Nginx - What now? 我在这个问题上非常喜欢Node.js + Nginx - 现在怎么样?

EDIT 2 编辑2

Here is my current configuration. 这是我目前的配置。
Nginx: 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) Node.js(服务器)

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 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. Nginx未配置为充当nodejs的反向代理。 Both php and nodejs requests should go through same port (Same origin policy) on nginx. php和nodejs请求都应该通过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. 要调试这样的问题,您应该使用telnet和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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在现有页面上使用Socket.io和Node.js(使用Nginx)吗? - Use Socket.io and Node.js on existing page (with nginx)? 集成node.js,socket.io和php网页 - Integrating node.js, socket.io and php web page 使用Socket.IO / Node.JS将通知推送到PHP - Push Notifications to PHP using Socket.IO / Node.JS node.js socket.io&memcached-使用PHP会话ID作为Socket.IO昵称 - node.js socket.io & memcached - using PHP Session ID as Socket.IO Nickname 如何在带有Node.js / Socket.IO的PHP中使用popen? - How can I use popen in PHP with Node.js/Socket.IO? socket.io node.js与简单的node.js ajax与php / ajax(用于数据) - socket.io node.js vs simple node.js ajax vs php/ajax (for data) 我如何或应该如何微调 Node.js/Socket.io 聊天应用程序,使其不会拖累我的整个网站/服务器? - How Can I or Should I Fine Tune Node.js/Socket.io Chat App So It Doesn't Drag Down My Entire Website/Server? Node.JS(+ Socket.io)和PHP的结合,用户和更改Socket ID的情况如何? - Combination of Node.JS(+ Socket.io) and PHP, what about users and changing Socket ID's 我们如何在不使用node.js的情况下使用socket.io进行核心php中的实时聊天对话 - How can we use socket.io for real time chat conversation in core php without using node.js PHP与除Apache外运行的Node.js(Socket.io)通信 - PHP communicates with Node.js (Socket.io) running besides Apache
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM