即使在代理服务备份后,mod_proxy仍会返回503个错误

我有一个设置与Apache2作为前端服务器的多个python应用程序gunicorn服务 。 我使用mod_proxy的Apache2设置如下所示:

<VirtualHost *:80> ServerName example.com UseCanonicalName On ServerAdmin webmaster@localhost LogLevel warn CustomLog /var/log/apache2/example.com/access.log combined ErrorLog /var/log/apache2/example.com/error.log ServerSignature On Alias /media/ /home/example/example.com/pysrc/project/media/ ProxyPass /media/ ! ProxyPass / http://127.0.0.1:4711/ ProxyPassReverse / http://127.0.0.1:4711/ ProxyPreserveHost On ProxyErrorOverride Off </VirtualHost> 

一般来说,这个设置工作得很好。 我有一个问题,但是:当我重新启动gunicorn进程(需要2-5秒),并有一个来自Apache的请求,该请求将失败,503错误。 到现在为止还挺好。 但是,即使在gunicorn进程恢复之后,Apache仍然会返回503个错误。 只有在完全重新启动Apache之后,它才从代理服务器恢复提供内容。

有没有办法解决这个问题?

retry=0添加到您的ProxyPass行:

 ProxyPass / http://127.0.0.1:4711/ retry=0 

从mod_proxy文档 :

连接池工作人员重试超时秒数。 如果连接池工作者到后端服务器处于错误状态,Apache将不会转发任何请求到该服务器,直到超时到期。 这可以closures后端服务器进行维护,并在稍后重新联机。 值为0意味着总是重试工作在错误状态,没有超时。

你是否按照logging的方法重新启动gunicorn?

我会推荐一个简单的方法。 如果在您的环境中2-5秒是可以接受的停机时间,那么我可以build议您简单地编写Apache服务脚本,在您重新启动gunicorn服务后立即重新启动?

在生产环境中,我build议使用HAProxy而不是Apache作为前端,并且运气可能会好得多。