gpt4 book ai didi

php - Socket.io 无法在 nginx + node.js + php 应用程序中连接

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:40:45 25 4
gpt4 key购买 nike

我正在尝试将 nginx 与 PHP 应用程序和 node.js 一起运行(这部分工作正常)。此外,我想将 socket.io 添加到此设置中,但不幸的是我无法在客户端和服务器之间建立连接(看起来连接超时?)。

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>

(我尝试了很多 url 变体。有/没有 http、127.0.0.1:3000、myappp.dev 等)

这是我当前的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;
}

但我也尝试了一些其他配置,例如:

Node.js + Nginx - What now?
https://www.digitalocean.com/community/questions/running-both-php-and-node-js-on-nginx-on-the-same-server

当我运行时

DEBUG=socket.io:* nodemon --debug test.js

我明白了

[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

没有错误,什么都没有。
知道我做错了什么吗?基本上我想做这样的事情https://github.com/jdutheil/nodePHP .但我无法让它工作。唯一的区别是来自 github 的应用程序使用的是 express 框架。我没有。

我很确定端口 3000 在我的服务器上是打开的(顺便说一句,它是无用的)

编辑:

这是我的 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 服务器

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 客户端

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

现在我连 PHP 都无法运行。我收到 404 HTTP 错误,空白页面包含文本:

Cannot GET / 

我非常喜欢这个问题Node.js + Nginx - What now?

编辑 2

这是我当前的配置。
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(服务器)

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

客户端.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 未配置为充当 nodejs 的反向代理。 php 和 nodejs 请求都应该通过 nginx 上的相同端口(同源策略)。你错过了这样的东西(检查你的教程)

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

还有一点。要调试此类问题,您应该使用 telnet 和 tcpdump。

在服务器上,你监听传入的连接

tcpdump -i eth0 'port 80'

在客户端,您测试并发出请求

telnet mysrv.com 80

关于php - Socket.io 无法在 nginx + node.js + php 应用程序中连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37386295/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com