将 Apache 的代理请求连接到 node.js 应用程序时出现尴尬的延迟

将 Apache 的代理请求连接到 node.js 应用程序时出现尴尬的延迟

在我的 Ubuntu Server 10.04 中,我正在运行一个示例 node.js 应用程序:

var http = require("http");

function onRequest(request, response) {

        console.log("Request received.");
        response.writeHead(200, {"Content-Type": "text/html"});
        response.write("Hello World");
        response.end();

}

http.createServer(onRequest).listen(3000);

它只是监听端口 3000 上的请求,登录控制台并向客户端发送 HTTP“你好世界”

目标是使此应用程序与 Apache2 共存。因此,经过一番研究,我编辑了默认文件 (/etc/apache2/站点可用/默认) 这边走:

<VirtualHost *:80>
        ServerAdmin [email protected]
        ServerName dev.myserver.com

        <Location /node>

                ProxyPassReverse http://127.0.0.1:3000/
                ProxyPass http://127.0.0.1:3000/

        </Location>

        <Proxy>
                Allow from all
        </Proxy>

        DocumentRoot /home/haj/www/http_home

        <Directory />
                Options FollowSymLinks
                AllowOverride None
        </Directory>

        <Directory /home/haj/www/http_home/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
        </Directory>

        ErrorLog /home/haj/www/log/error.log

        # Possible values include: debug, info, notice, warn, error, crit,
        # alert, emerg.
        LogLevel warn

        CustomLog /home/haj/www/log/access.log combined


</VirtualHost>

因此,当用户调用http://dev.myserver.com/nodehttp://dev.myserver.com/node/时,Apache 会代理请求,Node.js 会进行处理,然后用户会返回“Hello World”。

只有一个问题:它需要一些时间,我们称之为“加载”代理,换句话说,我在浏览器中收到这些消息:

<503 Service Temporarily Unavailable>

Service Temporarily Unavailable

The server is temporarily unable to service your request due to maintenance downtime or capacity problems. Please try again later.

Apache/2.2.14 (Ubuntu) Server at dev.neosource.cl Port 80

此外,error.log 告诉我:

[Thu Apr 21 01:02:10 2011] [error] proxy: HTTP: disabled connection for (127.0.0.1)
[Thu Apr 21 01:02:10 2011] [error] proxy: HTTP: disabled connection for (127.0.0.1)
[Thu Apr 21 01:02:10 2011] [error] proxy: HTTP: disabled connection for (127.0.0.1)

然后突然间,它就成功了,就像这样。不需要请求任何数量,不需要时间,也没有任何模式。

通俗地说,该服务必须'加载',这是它给人的印象,但是,我想知道是否有办法将这种延迟降到最低。或者我宁愿知道什么上述配置有错误。

编辑1:将 LogLevel 修改为调试后,在其中一次延迟期间,我根据请求获得此信息:

[Thu Apr 21 02:30:40 2011] [debug] mod_proxy_http.c(56): proxy: HTTP: canonicalising URL //127.0.0.1:3000/
[Thu Apr 21 02:30:40 2011] [debug] proxy_util.c(1494): [client 200.xxx.yyy.zzz] proxy: http: found worker http://127.0.0.1:3000/ for http://127.0.0.1:3000/
[Thu Apr 21 02:30:40 2011] [debug] mod_proxy.c(993): Running scheme http handler (attempt 0)
[Thu Apr 21 02:30:40 2011] [debug] mod_proxy_http.c(1940): proxy: HTTP: serving URL http://127.0.0.1:3000/
[Thu Apr 21 02:30:40 2011] [debug] proxy_util.c(1937): proxy: HTTP: retrying the worker for (127.0.0.1)
[Thu Apr 21 02:30:40 2011] [error] proxy: HTTP: disabled connection for (127.0.0.1)
[Thu Apr 21 02:30:40 2011] [debug] mod_deflate.c(615): [client 200.xxx.yyy.zzz] Zlib: Compressed 405 to 273 : URL /node

...当它再次正确时:

[Thu Apr 21 02:35:16 2011] [debug] mod_proxy_http.c(56): proxy: HTTP: canonicalising URL //127.0.0.1:3000/
[Thu Apr 21 02:35:16 2011] [debug] proxy_util.c(1494): [client 200.xxx.yyy.zzz] proxy: http: found worker http://127.0.0.1:3000/ for http://127.0.0.1:3000/
[Thu Apr 21 02:35:16 2011] [debug] mod_proxy.c(993): Running scheme http handler (attempt 0)
[Thu Apr 21 02:35:16 2011] [debug] mod_proxy_http.c(1940): proxy: HTTP: serving URL http://127.0.0.1:3000/
[Thu Apr 21 02:35:16 2011] [debug] proxy_util.c(1999): proxy: HTTP: has acquired connection for (127.0.0.1)
[Thu Apr 21 02:35:16 2011] [debug] proxy_util.c(2055): proxy: connecting http://127.0.0.1:3000/ to 127.0.0.1:3000
[Thu Apr 21 02:35:16 2011] [debug] proxy_util.c(2153): proxy: connected / to 127.0.0.1:3000
[Thu Apr 21 02:35:16 2011] [debug] proxy_util.c(2244): proxy: HTTP: backend socket is disconnected.
[Thu Apr 21 02:35:16 2011] [debug] proxy_util.c(2308): proxy: HTTP: fam 2 socket created to connect to 127.0.0.1
[Thu Apr 21 02:35:16 2011] [debug] proxy_util.c(2414): proxy: HTTP: connection complete to 127.0.0.1:3000 (127.0.0.1)
[Thu Apr 21 02:35:16 2011] [debug] mod_proxy_http.c(1723): proxy: start body send
[Thu Apr 21 02:35:16 2011] [debug] mod_deflate.c(615): [client 200.xxx.yyy.zzz] Zlib: Compressed 11 to 13 : URL /node
[Thu Apr 21 02:35:16 2011] [debug] mod_proxy_http.c(1816): proxy: end body send
[Thu Apr 21 02:35:16 2011] [debug] proxy_util.c(2017): proxy: HTTP: has released connection for (127.0.0.1)

答案1

更改LogLeveldebug将在 error.log 中为您提供更多信息。请执行此操作并发布结果。如果没有这些信息,我猜将 ProxyPass 行更改为ProxyPass http://127.0.0.1:3000/ retry=0可能会有所帮助。一般来说,Apache mod_proxy 文档包含可供您使用的参数的更多详细信息。

答案2

深入研究一下。我在启用了 SELinux 的 CentOS 上遇到了这个问题。我需要做的就是允许 httpd 建立网络连接:

/usr/sbin/setsebool httpd_can_network_connect 1

(并重新启动 httpd)

相关内容