[英]how to use node.js and php on the same port
我在端口12234上安装了node.js,socket.io和express.js,这非常好用。 但是现在我想在该端口(12234)的页面上使用php,但我不知道该怎么做。 有人有解决方案吗?
抱歉我的英语不好,我是荷兰人,英语不太好。
您无法在同一端口上运行两个应用程序。 最简单的方法是将HTTP请求代理到另一个端口上运行的PHP,并将其他请求代理到另一个端口上运行的Socket.IO。
这是使用http-proxy
的示例。 请注意,这不适用于flashsockets和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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.