在多服务器环境中运行 django 站点 - 如何处理会话?
我的 django 站点位于基于 Apache 的负载均衡器后面。还有一个处理静态文件请求的服务器,但这很简单。
让我困扰的是如何处理可以被负载均衡器扔到任何应用程序服务器的用户?它们都共享相同的数据库集群(这很聪明吗?或者我也应该在这里将其分开吗?),所以我唯一需要关心的是会话。
现在,为了提高性能,会话存储在内存中。我宁愿将它们留在那里,所以我需要做的是确保同一用户始终访问同一应用程序服务器。
我想到了一些简单的解决方案。
1) 使其基于IP。假设奇数 IP 编号访问服务器 A
,偶数 IP 编号访问服务器 B
。但是,如果用户位于代理后面或者 IP 经常变化怎么办?
2)当请求到达负载平衡服务器时,我可以尝试检查它的 cookie 并查看哪个服务器应该处理该请求。这可能/有意义吗?
3)我知道 J2EE 解决方案使用 mod_proxy_ajp
来处理此类情况,尽管我不知道幕后发生了什么。那里的东西可以用于 Django 吗?
或者也许有其他方法来集群 Django?
My django-powered site sits behind a Apache-based loadbalancer. There also is a server handling static file requests, but that's simple.
What bothers me is how to handle a user who can be thrown to any of the application servers by the load-balancer? They all share the same DB-cluster (is it smart? Or should I separate it here too?), so only thing I need to care about is the session.
Right now the sessions are stored in memory, for performance. I would rather keep them there, so what I'd need to do is make sure that the same user would always hit the same app-server.
A few simple solutions came to my mind.
1) make it IP-based. Lets say, odd IP numbers hit server A
and even numbers hit the B
. But what if a user is behind a proxy or has IP that changes often?
2) When a request hits the load-balancing server, I could try to check it for cookies and see which server should handle that. Is it possible / does make sense?
3) I know J2EE solutions, using mod_proxy_ajp
, handle those kind of situations, although I got no idea what happens behind the scenes there. Could something from there be used for Django?
Or maybe there is some other way to clusterize Django for that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以配置一个 memcached 集群并设置 django 以将其用于会话存储:
http://docs.djangoproject.com/en/ dev/topics/http/sessions/#using-cached-sessions
You could configure a cluster of memcached and setup your django to use it for session storage:
http://docs.djangoproject.com/en/dev/topics/http/sessions/#using-cached-sessions