小编典典

flask会话不持久

flask

在CentOS 6.3上与Python 2.7,Apache + mod_wsgi一起运行

当我在localhost上时,一切正常。但是,当我在Azure中的vm上运行代码时,我看不到会话信息在页面之间保留。

基本上我的看法是:

@frontend.route('/')
def index():
   session['foo'] = 'bar'
   print session['foo']

   return redirect(url_for("frontend.page2"))

@frontend.route('page2')
def page2():
   print session

打印输出为:

bar
<SecureCookieSession {}>

我对apache的wsgi配置是:

WSGISocketPrefix /var/run/wsgi

<VirtualHost *:80>
    ServerName example.com
    ServerAlias example.com

    WSGIDaemonProcess myproj threads=5 processes=5
    WSGIScriptAlias / /home/mydir/myproj/apache/myproj.wsgi

    <Directory /home/mydir/myproj>
        WSGIScriptReloading On
        WSGIProcessGroup myproj
        WSGIApplicationGroup %{GLOBAL}
        Order deny,allow
        Allow from all
    </Directory>
</VirtualHost>

我设置了secret_key:

app.secret_key = os.urandom(24)

我尝试同时设置SERVER_NAME,但这无济于事:

app.config['SERVER_NAME'] = 'example.com' 

关于如何进行更多调试的任何想法?


阅读 401

收藏
2020-04-07

共1个答案

小编典典

不要使用app.secret_key = os.urandom(24)!

你应该在此处输入一个静态值,而不是os.urandom每次都读取。你可能已经误解了docs中的示例,它向你展示了如何从中读取随机数据os.urandom,但它还明确指出:

只要拿起那东西并将其复制/粘贴到你的代码中就可以了

如果你在运行时阅读它,那么你的每个工作进程都将具有不同的密钥!这意味着,如果请求是由其他工作人员处理的,则会话将中断,因为cookie使用错误的密钥进行了签名。

2020-04-07