问题描述
我正在使用自己的基于nginx的RamSession的会话运行cherrypy应用程序.问题在于每个请求的会话ID都会更改.我相信问题在于每次发出请求时,它都会转到另一个工作人员,因此会话得以保存,但是下一个可用工作人员在下一个请求中无法识别该会话(不幸的是,人们对事情的工作方式知之甚少).当我将工人数设置为1时,一切都会按预期进行.我知道我可能可以使用FileSession或任何类型的基于DB的会话处理程序,但只是想知道是否有解决方案.谢谢
I am running a cherrypy application using my own session based of RamSession behind nginx. The problem is the session id changes on every request. I believe the problem is every time a request is made it goes to a different worker and thus the session is saved, but it is not recognized in the next request by the next available worker (limited knowledge on how things work unfortunately). When I set the number of workers to 1 then everything works as expected. I know I can probably use FileSession or any type of DB based session handler, but just wanted to know if there is a solution for this. Thanks
这是我的暴发户脚本:
description "uwsgi tiny instance"
start on runlevel [12345]
stop on runlevel [06]
exec /home/web/.virtualenvs/myenv/bin/uwsgi --uid web -H /home/web/.virtualenvs/myenv -w myapp.wsgi -p 1 -M -s 127.0.0.1:3031
这是我的课程:
class MySession(sessions.RamSession):
def clean_up(self):
"""Clean up expired sessions."""
now = self.now()
for id, (data, expiration_time) in copyitems(self.cache):
if expiration_time <= now:
try:
active = Mongo(ActiveSession).find_one('active', self.cache['active'])
Mongo(ActiveSession).remove(active)
except:
print "Failed to remove active session object."
try:
del self.cache[id]
except KeyError:
pass
try:
del self.locks[id]
except KeyError:
pass
# added to remove obsolete lock objects
for id in list(self.locks):
if id not in self.cache:
self.locks.pop(id, None)
和我的配置:
config = {
'/static': {
'tools.staticdir.on': True,
'tools.staticdir.dir': os.path.join(current_dir, 'media/public')
},
'/fotos': {
'tools.staticdir.on': True,
'tools.staticdir.dir': os.path.join(current_dir, 'media/fotos')
},
'/' : {
'tools.sessions.on': True,
'tools.sessions.name': 'myapp'
'tools.sessions.storage_type': 'my',
'engine.autoreload_on': False
}
}
推荐答案
您的直觉是正确的:RamSession一次限制为1个进程.一个简单的解决方案是切换到FileSession(如果您的工作人员都可以访问同一文件系统)或一个DB会话.假设您的工人分布很分散,很可能是后者.
Your intuition is correct: the RamSession is limited to 1 process at a time. The simple solution would be to switch to FileSession (if your workers all have access to the same filesystem) or a DB session. Assuming your workers are heavily distributed, most likely the latter.
这篇关于在nginx后面使用带有樱桃的会话RamSession时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!