简体   繁体   English

Apache ProxyPass和Node.js-不提供socket.io

[英]Apache ProxyPass and Node.js - not serving up socket.io

I'm trying to run a Node.js alongside my existing apache and I have a ProxyPass problem when serving content from my Express.js server. 我正在尝试与现有的apache一起运行Node.js,从Express.js服务器提供内容时出现ProxyPass问题。 What's seems to be the trouble is the request that the Node server sees when it is forwarded by Apache. 问题似乎出在Node服务器被Apache转发时看到的请求。

I tried this config: 我尝试了这个配置:

<VirtualHost *:80>
    DocumentRoot "/var/www/html"
    ProxyPreserveHost on
    ProxyPass /node http://localhost:3000/
    ProxyPassReverse /node http://localhost:3000/
</VirtualHost>

Node is setup the server like such: 节点像这样设置服务器:

var express = require('express')
, app = express()
, server = require('http').createServer(app)
, io = require("socket.io").listen(server) 

app.use(function(req, res, next){
    console.log(req.method, req.url); // log the method and route
    next();
});
app.use(express.static(__dirname + '/public'));
app.use('/components', express.static(__dirname + '/components'));
app.use('/js', express.static(__dirname + '/js'));

I get this logged by node: 我得到此记录的节点:

GET /
GET //components/bootstrap/dist/css/bootstrap.css
GET //components/font-awesome/css/font-awesome.css
GET //css/flags.css
GET //css/app.css
GET //components/jquery/dist/jquery.js
GET //components/bootstrap/dist/js/bootstrap.js
GET //socket.io/socket.io.js
GET //js/client.js
GET //components/bootstrap/dist/css/bootstrap.css.map

Question: I am getting an extra slash when apache sends the request and the socket.io library is not served. 问:当apache发送请求并且不提供socket.io库时,我得到一个额外的斜线。 How can I make Apache remove leading slashes before they get to node.js? 如何使Apache在到达斜线斜杠之前先删除斜杠?

I ended up going with HAProxy to sit in front of node and apache; 我最终选择了HAProxy来坐在node和apache的前面; as suggested in the comments above. 如以上评论中所建议。

These are the settings I went with: 这些是我使用的设置:

 frontend main
    bind *:80
    mode http
    default_backend apache

    acl is_node path_beg -i /node/
    use_backend node if is_node

    acl is_node_too path_beg -i /node
    use_backend node if is_node_too

    acl is_socket path_beg -i /socket.io/
    use_backend socketio if is_socket

    acl is_socketio_too path_beg -i /socket.io
    use_backend socketio if is_socketio_too

backend apache
    mode http
    balance     roundrobin
    server      apache localhost:81

backend node
    mode http
    balance     roundrobin
    server      node localhost:3000
    reqrep ^([^\ :]*)\ /node/(.*)     \1\ /\2
    reqrep ^([^\ :]*)\ /node(.*)     \1\ /\2

backend socketio
    mode http
    balance     roundrobin
    server      node localhost:3000
    reqrep ^([^\ :]*)\ /node/(.*)     \1\ /\2
    reqrep ^([^\ :]*)\ /node(.*)     \1\ /\2

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM