Akward延迟将Apache的代理请求连接到node.js应用


12

在我的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 “ Hello World”

目的是使该应用程序与Apache2共存。因此,经过一些研究,我以这种方式编辑了默认文件(/ etc / apache2 / sites-available / default):

<VirtualHost *:80>
        ServerAdmin haj@myserver.com
        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/node或时http://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)

主持人:我不确定这是stackoverflow问题还是serverfault问题,因此我发布了两个网站。
Herman Junge

1
除非您没有答案,否则交叉发布是个坏主意,请确保也交叉链接它们之间!如果您在错误的位置发帖,版主将帮助您将其迁移到正确的位置,但请不要一开始就重复我们的工作!
Caleb

抱歉@Caleb,初学者的错误。不会再发生。
Herman Junge,

不用担心,这是您学习的地方。
Caleb

Answers:


10

更改LogLeveldebug将在error.log中提供更多信息。请这样做并发布结果。没有这些信息,我猜测将ProxyPass行更改为ProxyPass http://127.0.0.1:3000/ retry=0可能会有所帮助。通常,Apache mod_proxy文档提供了有关可用参数的更多详细信息。


感谢@justarobert,现在我遇到了“逆墨菲”,因为我正在尝试复制错误,一切正常!我敢打赌,在生产中我将再次遇到这种情况……一旦有了数据,我将在这里填写它。再次感谢。
Herman Junge

那个数字!确保不要LogLevel debug在生产中使用。
justarobert

大声笑。我刚刚编辑了我的问题。
Herman Junge

1
Apache日志表明延迟期间您的node.js进程未响应代理请求。您当时从node.js日志中得到了什么?当Apache给您延迟时,您可以直接在浏览器中访问node.js服务器吗?考虑使用ProxyPass http://127.0.0.1:3000/ retry=0 timeout=10,只是为了使超时明确。另外,请参阅news.ycombinator.com/item?id=2037328上的讨论,了解为什么您可能不想将Apache放在node.js的前面。
justarobert

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.