Linux下openresty+redis 的权限控制,openrestyredis
Linux下openresty+redis 的权限控制,openrestyredis
前些日子研究了一下nginx,也基于它的源码编写了一些程序,但是发现这种“原始”的做法即使是一个简单的HelloWorld程序就非常的耗费时间。后来发现了openresty这个东西,它是前人集成了nginx和lua的,nginx的高阶版本。lua是一种小巧的,高级的脚本语言,能够很方便地在nginx的配置文件中编写出自己的业务流程。
业务说明:
传统的权限控制,如果是J2EE,都会在spring中添加filter的管控,这在只有一台服务器里面,并没有什么问题,但是在集群的世界里,这显然不怎么合理。我们不希望我们的上游服务器来处理权限的问题,而是在权限信息到达之前就已经处理好了,上游服务器只要跑数据,处理业务就行。于是权限控制这一功能模块,就放在负责分发任务的nginx身上,如果你是用nginx来搭建集群的话。。。
准备:
操作系统:centos,ubuntu感觉不怎么好用,因为动不动就会提示容量不足,本人也是不信邪,一次又一次的重装ubuntu,后来干脆换centos。
功能软件,如下图,这是我的虚拟机上的版本。
1、openresty-1.11.2.5 安装方法参照 https://openresty.org/cn/ ,安装完成后,就已经是nginx+lua了。最好是下载源码版,,然后自己编译一下,还是很爽的。
2、Redis-3.2.10 下载地址 https://redis.io/ 解压压缩包,然后打开解压后的目录,编译安装(网上很多教程)
3、lua-resty-http-0.11 这是个开源的第三方包,用于处理http的,就是访问地址用的,下载地址是 https://github.com/pintsized/lua-resty-http/ 虽然openresty本身也有,但是亲测之后并不好用
注意上面都是一堆源码,真正安装后可能不是在这个路径下了
前两个的安装过程相对独立,没什么好说的,找找资料就可以,关键说一下lua-rety-http的集成过程,打开文件夹找到下面两个文件,拷贝这两个文件
然后去到openresty安装完成后的地址,粘贴到这个文件夹下,准备工作到这里就结束了
openresty实际上也是nginx,我们依然可以在openresty的安装目录下,找到nginx的文件夹,这部分是独立,可以完全可以当作一般nginx那样启动,然后是代码。
先启动 redis
[plain] view plain copy
- [root@localhost sbin]# ./redis-server /home/sword/Downloads/redis-3.2.10/redis.conf
然后修改nginx.conf
[cpp] view plain copy
- #user nobody;
- worker_processes 1;
- #error_log logs/error.log;
- #error_log logs/error.log notice;
- #error_log logs/error.log info;
- #pid logs/nginx.pid;
- events {
- worker_connections 1024;
- }
- http {
- include mime.types;
- default_type text/html;
- lua_package_path "/usr/local/openresty/lualib/?.lua;;"; #使resty.http生效
- lua_package_cpath "/usr/local/openresty/lualib/?.so;;";
- #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
- # '$status $body_bytes_sent "$http_referer" '
- # '"$http_user_agent" "$http_x_forwarded_for"';
- #access_log logs/access.log main;
- upstream swordnet.com{
- server 132.121.164.203:8280;
- }
- sendfile on;
- #tcp_nopush on;
- #keepalive_timeout 0;
- keepalive_timeout 65;
- #gzip on;
- server {
- listen 80;
- server_name localhost;
- charset utf-8;
- #access_log logs/host.access.log main;
- location ~ .*/(token/[^/|\?]*).*{#(1)、在客户端输入的url,只要是符合 http://localhost/xxx/token/xxx的都会在这里被捕获
- set $token $1;#(2)、识别(1)中的正则表达式,取出()中的内容,并赋值给$token
- content_by_lua_block {
- local http = require "resty.http";#导入resty.http和resty.redis包
- local redis = require "resty.redis";
- local httpc = http.new();
- local instance = redis.new();
- local var = ngx.var;#使用nginx自带的变量池
- local token = var.token;#使用(2)中的$token 这里是token/swordnet123
- local ok,err = instance:connect("127.0.0.1","6379");#连接redis
- if not ok then
- ngx.print("error!!!");
- end
- instance:set("token/swordnet123","http://127.0.0.1:81/xxx/zzz/showData?entityid=4406041007625");#往redis中添加测试数据
- instance:set("wto78425@$12Gz",1);
- local urlc = instance:get(token);#(3)、取出 http://127.0.0.1:81/xxx/zzz/showData?entityid=4406041007625,观察这个连接的域,依然是nginx的ip和port
- if urlc == ngx.null then
- ngx.print("error");
- else
- local res, err = httpc:request_uri(urlc,{ method = "GET",});#发出新的请求
- ngx.print(res.body);
- end;
- }
- }
- }
- server {
- listen 81;
- server_name localhost;
- charset utf-8;
- #access_log logs/host.access.log main;
- location /xxx {(3)中发出的新请求会在这里被捕获
- proxy_pass http://swordnet.com;
- }
- }
- # another virtual host using mix of IP-, name-, and port-based configuration
- #
- #server {
- # listen 8000;
- # listen somename:8080;
- # server_name somename alias another.alias;
- # location / {
- # root html;
- # index index.html index.htm;
- # }
- #}
- # HTTPS server
- #
- #server {
- # listen 443 ssl;
- # server_name localhost;
- # ssl_certificate cert.pem;
- # ssl_certificate_key cert.key;
- # ssl_session_cache shared:SSL:1m;
- # ssl_session_timeout 5m;
- # ssl_ciphers HIGH:!aNULL:!MD5;
- # ssl_prefer_server_ciphers on;
- # location / {
- # root html;
- # index index.html index.htm;
- # }
- #}
- }
最后启动nginx,在浏览器虽然输入个什么http://localhost/tt/token/swordnet123测试一下结果。
评论暂时关闭