背景

目前我在集成登录认证功能cas),使用架构nginx+lua,由于我们有多个系统(全是前端项目),每套系统都采用nginx+lua方式进行部署(即每个系统都是一个nginx),cas登录认证使用到了nginx缓存机制,现在的问题在于这么一个场景

有两个项目A、B,两个使用的是同一个域名C,如下图:

在这里插入图片描述

问题所在

用户的角度是:AB是一套系统,我登录了A,到B系统我就不会在登录了…

开发角度:因为A、B是两套nginx登录认证使用到了nginx缓存,这就造成一个问题访问A的使用用的是A系统缓存访问B系统使用的是B系统缓存;即便在A系统登录了,但是到B系统,由于B系统没有缓存,就会造成在登录一次;

解决方案

对于上述的问题,目前我想到了下面这几种方案,各有优缺点,选择自己最合适的就行;

  1. 引入redis集中缓存
  2. 使用共享磁盘,使用一个nginx缓存路由的时候,路由共享磁盘的绝对路径上;
  3. 更改路径,在使用缓存地址上,分别路由到各自的缓存上,分别使用各自的缓存缺陷,如果使用cookie的话,会频繁刷新cookie,并且会给服务带来性能压力)
  4. 项目部署同一个nginx上(缺点:部署发布的时候,流程特别慢!nginx要是炸的话,项目全炸)

方案一:redis

统一使用redis缓存

缓存使用一个集中式的,就可以避免上述的问题,而且还顺带解决了单点登录问题!!

优点:

缺点:

方案二:共享磁盘

这种方式是,使用一个nginx的缓存,在具体路由B项目的时候,路由共享磁盘的绝对路径上;

相当于是B项目部署只是为了将内容放到共享磁盘里面,真正走的只有A系统,通过location路由共享磁盘目录就行

A项目的nginx配置示例

server {
    listen 8080;
    server_name ~^test-(?<subdomain&gt;.+).aaaa.com;
    # 禁止转发时携带端口
    port_in_redirect off;
    client_body_buffer_size 1024m;
    client_max_body_size    1024m;
    gzip on;
    gzip_min_length 1k;
    gzip_comp_level 2;
    gzip_types text/plain text/json application/javascript application/x-javascript text/css application/xml text/javascript font/ttf font/otf image/svg+xml;
    gzip_vary on;

    set $hpath '/usr/share/nginx/html/dist/$subdomain/';
    
    # 访问B项目的时候,在这里写死共享磁盘路径
    location /info {
        set $hpath '/home/dist/bdcInfo/';
     
        resolver 8.8.8.8;     
        # access_log /home/cc.log main;
        # error_log /home/ww.log;   
        access_by_lua_file /etc/nginx/cas.lua;
        default_type text/plain;
        alias $hpath;
        index index.html;
        try_files $uri $uri/  /index.html;
    }
    location / {
        resolver 8.8.8.8;     
        # access_log /home/cc.log main;
        # error_log /home/ww.log;   
        access_by_lua_file /etc/nginx/cas.lua;
        default_type text/plain;
        root $hpath;
        index index.html;
        try_files $uri $uri/  /index.html;
    }
    
    location /currentUser {
        default_type text/html;
        charset utf-8;
        # access_log /home/base-station-test11.log main;
        # error_log /home/qq.log;
        access_by_lua_file /etc/nginx/current_user.lua;
    }
    
    proxy_intercept_errors  on;
    error_page 404 400 403 500 502  = /404.html;
    location = /404.html {
        root /home/resource;
    }
}

优点:

缺点:

方案三:配置B的登录路由

需要前端配合请求权限接口修改为B的前缀,我这里使用到的登录接口为currentUser

nginx示例配置

server {
    listen 8080;
    server_name ~^test-(?<subdomain>.+).aaaa.com;
    # 禁止转发时携带端口
    port_in_redirect off;
    client_body_buffer_size 1024m;
    client_max_body_size    1024m;
    gzip on;
    gzip_min_length 1k;
    gzip_comp_level 2;
    gzip_types text/plain text/json application/javascript application/x-javascript text/css application/xml text/javascript font/ttf font/otf image/svg+xml;
    gzip_vary on;

    set $hpath '/usr/share/nginx/html/dist/$subdomain/';
    
    # 访问B项目的时候,在这里写死共享磁盘路径
    location /info {
        set $hpath '/home/dist/bdcInfo/';
     
        resolver 8.8.8.8;     
        # access_log /home/cc.log main;
        # error_log /home/ww.log;   
        access_by_lua_file /etc/nginx/cas.lua;
        default_type text/plain;
        alias $hpath;
        index index.html;
        try_files $uri $uri/  /index.html;
    }
    # 新增B请求登录的接口前缀
    location /info/currentUser {
        default_type text/html;
        charset utf-8;
        # access_log /home/base-station-test11.log main;
        # error_log /home/qq.log;
        access_by_lua_file /etc/nginx/lua/cas-auth/current_user.lua;
    }
    
    location / {
        resolver 8.8.8.8;     
        # access_log /home/cc.log main;
        # error_log /home/ww.log;   
        access_by_lua_file /etc/nginx/cas.lua;
        default_type text/plain;
        root $hpath;
        index index.html;
        try_files $uri $uri/  /index.html;
    }
    
    location /currentUser {
        default_type text/html;
        charset utf-8;
        # access_log /home/base-station-test11.log main;
        # error_log /home/qq.log;
        access_by_lua_file /etc/nginx/current_user.lua;
    }
    
    proxy_intercept_errors  on;
    error_page 404 400 403 500 502  = /404.html;
    location = /404.html {
        root /home/resource;
    }
}

这种方案成本比较低,但是缺点也很明显;

这种在请求A的时候,会走登录,请求B的时候,也会走登录,但是A,B域名一致,就导致A,B的cookie来回重刷,比较耗费性能!

这里还有个疑问点是:

为什么在登录了A之后,在请求B的时候为什么不跳登录页,而是直接走接口!!!(这里没弄懂)

就是我第一次登录的时候,访问A,A会跳转到登录页,然后输入用户密码之后,走了一系列的登录接口,登录成功;

这个时候在访问B ,B没有跳转到登录页,跟A一样,走的一模一样的登录接口,然后登录成功!!

是因为同一域名的问题么?????没搞懂!!

方案四:将A,B部署在同一nginx上

这个就不用解释了,但缺点很明显,一旦nginx出现问题,A,B项目都不能访问,而且在部署的时候,需要拉两套代码编译两套代码时间会很长

遗留问题

方案三最后的那几行,确实没想通,如果大家有什么想法的话,欢迎沟通哈!!!!

原文地址:https://blog.csdn.net/zz18435842675/article/details/134672401

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。

如若转载,请注明出处:http://www.7code.cn/show_4765.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注