gpt4 book ai didi

nginx - ASP.NET Core + Redis + nginx 的 session id 总是在变化

转载 作者:行者123 更新时间:2023-12-04 14:21:50 24 4
gpt4 key购买 nike

示例项目https://github.com/xingyu217/Redis-nginx-aspnetcore-session
nginx.config:

upstream study_server{
server localhost:8011 weight=1;
server localhost:8013 weight=1;
#server localhost:8014 weight=1;
#server 172.17.16.147:8012 weight=2;
#server 172.17.16.147:8011 weight=2;
}
server {
listen 80;
server_name localhost;

#charset koi8-r;

#access_log logs/host.access.log main;

location / {
#root html;
#index index.html index.htm;
proxy_pass http://study_server/;
proxy_cookie_path ~*^/.* /;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}

获取 session ID: ViewBag.seId= HttpContext.Session.Id ;

启动.cs:
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});

//services.AddDistributedMemoryCache();

services.AddDistributedRedisCache(options =>
{
options.Configuration = Configuration.GetConnectionString("RedisConnection");
options.InstanceName = "master";
});
services.AddSession(options =>
{
options.IdleTimeout = TimeSpan.FromSeconds(100);
//options.Cookie.HttpOnly = true;
});
//services.AddSession();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}

//app.UseDeveloperExceptionPage();
//app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseSession();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}

访问 URL: localhost 并反复刷新, session id 会一直在变化。

我检查了 redis 服务器中的 key ,它总是在刷新页面时生成新 key 。 (例如masterdef69307-fbd3-c6ed-91d2-009b2306f902)

如果我只使用服务器(本地主机:8011):
upstream study_server{
server localhost:8011 weight=1;
#server localhost:8013 weight=1;
#server localhost:8014 weight=1;
#server 172.17.16.147:8012 weight=2;
#server 172.17.16.147:8011 weight=2;
}

session ID 不会更改。

任何人都知道这将不胜感激。

谢谢

最佳答案

我实际上无法在具有配置的 mac 上重现该问题(2 个本地服务器通过 nginx 负载平衡并连接到相同的本地 redis 分布式缓存)

无论如何,如果适用,您是否尝试过启用 ip_hash在 nginx 上游模块中?

来自 nginx doc 中的 session 持久性部分:

If there is the need to tie a client to a particular application server — in other words, make the client’s session “sticky” or “persistent” in terms of always trying to select a particular server — the ip-hash load balancing mechanism can be used.


upstream study_server{
ip_hash;
server localhost:8011 weight=1;
server localhost:8013 weight=1;
#server localhost:8014 weight=1;
#server 172.17.16.147:8012 weight=2;
#server 172.17.16.147:8011 weight=2;
}

如果这不能解决您的问题,请查看堆栈交换上的类似问题:
  • https://serverfault.com/questions/133500/nginx-sticky-session-and-iis
  • ASP.NET: Session.SessionID changes between requests
  • 关于nginx - ASP.NET Core + Redis + nginx 的 session id 总是在变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59629622/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com