背景
目前我在集成登录认证功能(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系统没有缓存,就会造成在登录一次;
解决方案
对于上述的问题,目前我想到了下面这几种方案,各有优缺点,选择自己最合适的就行;
引入redis集中缓存;使用共享磁盘,使用一个nginx缓存,路由的时候,路由到共享磁盘的绝对路径上;更改路径,在使用缓存的地址上,分别路由到各自的缓存上,分别使用各自的缓存(缺陷,如果使用cookie的话,会频繁刷新cookie,并且会给服务带来性能压力)将项目部署同一个nginx上(缺点:部署发布的时候,流程特别慢!nginx要是炸的话,项目全炸) 方案一:redis
统一使用redis缓存;
将缓存使用一个集中式的,就可以避免上述的问题,而且还顺带解决了单点登录问题!!
优点:
解决了缓存共享值问题;解决单点登录问题;
缺点:
引入了redis,系统复杂度变高维护成本也上升; 方案二:共享磁盘
这种方式是,使用一个nginx的缓存,在具体路由B项目的时候,路由到共享磁盘的绝对路径上;
相当于是B项目的部署只是为了将内容放到共享磁盘里面,真正走的只有A系统,通过location段路由到共享磁盘目录就行
A项目的nginx配置示例:
server { listen 8080; server_name ~^test-(?
.+)\.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; } } 优点:
解决了上述问题;缺点:
需要运维引入共享磁盘,技术选型:nfs ,fastdfs,nas,增加运维成本 方案三:配置B的登录路由 需要前端配合请求权限的接口修改为B的前缀,我这里使用到的登录接口为currentUser
nginx示例配置
server { listen 8080; server_name ~^test-(?.+)\.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项目都不能访问,而且在部署的时候,需要拉两套代码,编译两套代码,时间会很长
遗留问题方案三最后的那几行,确实没想通,如果大家有什么想法的话,欢迎沟通哈!!!!