Sinatra 清除岗位上的会议 [英] Sinatra clears session on post

查看:34
本文介绍了Sinatra 清除岗位上的会议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

enable :sessions
set :session_secret, 'secret'

post '/login' do
        session[:loggedInUser] = jsondata['username'].to_s
        puts session[:loggedInUser] + " is the session"
end

此时一切都很好.当我像这样阅读会话时:

Everything is good at this point. When I read the session like this:

get '/debug' do
    session.inspect
end

它就在那里.但问题来了.当我稍后再提出另一个帖子请求时:

Its all there. But here comes the problem. When I go for another post request later on:

post '/foo' do
    # do nothing
end

会话已清除.

为什么?这是一个错误吗?

Why? Is this a bug?

编辑

我已经缩小了问题的范围:我通过 nginx 将 Sinatra 代理传递到 http://app.local/backend - 这就是问题发生的时间.如果我通过 http://localhost:4567 运行 Sinatra,它会按预期运行.

I have narrowed the problem down: I proxypass Sinatra through nginx, to http://app.local/backend - this is when the issue occurs. If I run Sinatra through http://localhost:4567 it all works as expected.

解决方案

使用 Rack::Session::Cookie 而不是默认的 enable :sessions:

Use Rack::Session::Cookie instead of the default enable :sessions:

use Rack::Session::Cookie, :key => "rack.session",
:path => "/backend"
# etc

来自 Sinatra 常见问题:

如果你需要为会话设置额外的参数,比如过期时间日期,直接使用 Rack::Session::Cookie 而不是启用 :sessions:

If you need to set additional parameters for sessions, like expiration date, use Rack::Session::Cookie directly instead of enable :sessions:

推荐答案

我遇到了和你一样的问题:会话在发布时被清除.

I was suffering from the same issue as you: sessions were being cleared on post.

我不知道为什么会这样,但这是我的解决方案:

I have no idea why this works, but this is my solution:

#enable :sessions
use Rack::Session::Cookie, :key => 'rack.session',
                           :path => '/',
                           :secret => 'your_secret'

我实际上只是用 use Rack::Session::Cookie ... 替换了 enable :sessions 位,现在一切都很好.

I literally just replaced the enable :sessions bit with use Rack::Session::Cookie ... and now all is good in the world.

这篇关于Sinatra 清除岗位上的会议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆