Apache 套接字未关闭?
我有一个使用 CherryPy 编写的 Web 应用程序,它在 127.0.0.1:4321
上本地运行。 我们使用 mod-rewrite 和 mod-proxy 让 Apache 充当反向代理; Apache 还处理我们的 SSL 加密,并最终可能用于传输我们所有的静态内容。
这对于小型工作负载来说效果很好。 不过,我最近使用 urllib2 编写了一个压力测试脚本,可以模拟 100 个客户端的工作负载。 一段时间后,每个客户端都会从 Apache 收到 503 错误,表明 Apache 无法连接到 127.0.0.1:4321
。 CherryPy 运行正常,但我的 Apache 错误日志显示如下行:
[Thu Oct 02 12:55:44 2008] [error] (OS 10048)每个套接字地址仅一次使用(协议/网络地址/端口)通常是允许的。 : proxy: HTTP: attempts to connect to 127.0.0.1:4321 (*) failed
谷歌搜索此错误表明 Apache 可能已用完套接字文件描述符。 由于我只有 100 个客户端在运行,这意味着我的 urllib2 连接和 Apache 之间的连接都没有关闭(我肯定是在urlopen
的返回值),或者在 Apache 和 CherryPy 之间。
我已确认我的 urllib2
请求正在发送 HTTP Connection: close
标头,尽管 Apache 配置为 KeepAlive On
(如果这很重要)。
以防万一,我正在使用 Python 2.5、Apache 2.2、CherryPy 3.0.3,并且服务器在 Windows Server 2003 上运行。
那么我下一步要做什么来解决这个问题呢?
I have a web application written using CherryPy, which is run locally on 127.0.0.1:4321
. We use mod-rewrite and mod-proxy to have Apache act as a reverse proxy; Apache also handles our SSL encryption and may eventually be used to transfer all of our static content.
This all works just fine for small workloads. However, I recently used urllib2
to write a stress-testing script that would simulate a workload of 100 clients. After some time, each client gets a 503 error from Apache, indicating that Apache cannot connect to 127.0.0.1:4321
. CherryPy is functioning properly, but my Apache error log reveals lines like the following:
[Thu Oct 02 12:55:44 2008] [error] (OS 10048)Only one usage of each socket address (protocol/network address/port) is normally permitted. : proxy: HTTP: attempt to connect to 127.0.0.1:4321 (*) failed
Googling for this error reveals that Apache has probably run out of socket file descriptors. Since I only have 100 clients running, this implies that the connections are not being closed, either between my urllib2
connection and Apache (I am definitely calling .close()
on the return value of urlopen
), or between Apache and CherryPy.
I've confirmed that my urllib2
request is sending an HTTP Connection: close
header, although Apache is configured with KeepAlive On
if that matters.
In case it matters, I'm using Python 2.5, Apache 2.2, CherryPy 3.0.3, and the server is running on Windows Server 2003.
So what's my next step to stop this problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
SetEnv proxy-nokeepalive 1
可能会立即告诉您问题是否出在 Apache 和 CP 之间的 keepalive 上。 有关详细信息,请参阅 mod_proxy 文档。SetEnv proxy-nokeepalive 1
would probably tell you right away if the problem is keepalive between Apache and CP. See the mod_proxy docs for more info.您可以运行 netstat 命令并查看是否有一堆处于 TIME_WAIT 状态的套接字。 根据您的 MaxUserPort 设置,您可以使用的端口数量可能会受到严重限制。 此外,TcpTimedWaitDelay 通常设置为 240 秒,因此使用的任何套接字在四分钟内都不能重复使用。
这里还有更多好信息 --> http://smallvoid.com/article/winnt-tcpip-max-limit.html
You might run the netstat command and see if you have a bunch of sockets in the TIME_WAIT state. Depending on your MaxUserPort setting you might be severly limited in the number of ports available to use. In addition the TcpTimedWaitDelay is usually set to 240 seconds so any sockets that are used cannot be reused for four minutes.
There's more good information here --> http://smallvoid.com/article/winnt-tcpip-max-limit.html