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

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

基本上我的看法是:

@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'

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

谢谢!

最佳答案

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

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



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

10-06 09:24
查看更多