如何在同一个端口上使用node.js和php

我已经在端口12234上安装了node.js,socket.io和express.js,并且工作得非常好。 但是现在我想在该端口的一个页面上使用php(12234),但我不知道如何才能做到这一点。 有没有人有解决办法?

对不起我的英语不好,我是荷兰人,英语不太好。

您不能在同一个端口上运行两个应用程序。 最简单的做法是向PHP代理HTTP请求,在另一个端口上运行,并将其他请求代理到另一个端口上运行的Socket.IO。

这里是一个使用http-proxy的例子。 请注意,这不适用于像闪存和XHR长轮询的事情。

 var httpProxy = require('http-proxy') var server = httpProxy.createServer(function (req, res, proxy) { proxy.proxyRequest(req, res, { host: 'localhost', port: // whatever port PHP is running on }); }) server.on('upgrade', function (req, socket, head) { server.proxy.proxyWebSocketRequest(req, socket, head, { host: 'localhost', port: // whatever port Socket.IO is running on }); }); server.listen(80); 

或者,您可以将您的Express路由路由到PHP。 如果PHP在端口8080上运行,例如:

 app.get('/', function(req, res) { // send a HTTP get request http.get({hostname: 'localhost', port: 8080, path:'/', function(res2) { // pipe the response stream res2.pipe(res); }).on('error', function(err) { console.log("Got error: " + err.message); }); }); 

您可以通过nginx将node.js作为上游运行。 提供你正在使用nginx和php5-fpm。

 upstream node { server 127.0.0.1:8080; } location / { proxy_pass http://node; proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504; proxy_redirect off; proxy_buffering off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "Upgrade"; } location ~ \.php$ { index index.php; root /var/www; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME /var/www/$fastcgi_script_name; }