简体   繁体   English

在nginx后面使用带有Cherrypy的会话RamSession时出错

[英]Error using session RamSession with cherrypy behind nginx

I am running a cherrypy application using my own session based of RamSession behind nginx. 我正在使用我自己基于nginx的RamSession的会话运行cherrypy应用程序。 The problem is the session id changes on every request. 问题在于每个请求的会话ID都会更改。 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. 当我将工人数设置为1时,一切都会按预期进行。 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. 我知道我可能可以使用FileSession或任何类型的基于数据库的会话处理程序,但只是想知道是否有解决方案。 Thanks 谢谢

Here is my upstart script: 这是我的暴发户脚本:

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

Here is my session: 这是我的会议:

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)

and my config: 和我的配置:

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
    }
}

Your intuition is correct: the RamSession is limited to 1 process at a time. 您的直觉是正确的:RamSession一次限制为1个进程。 The simple solution would be to switch to FileSession (if your workers all have access to the same filesystem) or a DB session. 简单的解决方案是切换到FileSession(如果您的工作人员都可以访问同一文件系统)或DB会话。 Assuming your workers are heavily distributed, most likely the latter. 假设您的工人分布很分散,很可能是后者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM