我试图用PHP app和node.js一起运行Nginx(这部分工作正常).另外我想将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
(我尝试过很多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的app正在使用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',{ 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',{ msg: 'Foo bar' } );
});
Client.js
var socket = io.connect('127.0.0.1:3000',{path: '/nodejsapi/'});
socket.on('msg',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