nginx lua 根据参数指向不同location
2021/11/17 7:11:20
本文主要是介绍nginx lua 根据参数指向不同location,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
正则匹配链接和获取参数,利用lua根据参数指向不同location 代码块如下:
server { listen 80; server_name loc.lua.com; index index.php index.html; root /var/www/course-front-api/public; location ~ ^(.*)/\.svn/ { return 404;break; } location = /request_body { client_max_body_size 50k; client_body_buffer_size 50k; content_by_lua_block { ngx.req.read_body() -- explicitly read the req body local data = ngx.req.get_body_data() if data then ngx.say("body data:") ngx.print(data) return end -- body may get buffered in a temp file: local file = ngx.req.get_body_file() if file then ngx.say("body is in file ", file) else ngx.say("no body found") end } } # rewrite ^/a_([0-9]+)\.html$ /index.php/?aid=$1 last; --匹配地址 location ~ ^/a_([0-9]+)\.html$ { default_type 'text/plain'; content_by_lua_block { local m, err = ngx.re.match(ngx.var.uri, "[0-9]+") if err then ngx.log(ngx.ERR, "error: ", err) return end -- ngx.say('Hello,world!',ngx.var.uri,m[0]) -- 可以根据自己的需求,查库或者redis local file = io.open("/var/www/aiops/docker/base_image/nginx/sites/id.txt",'r') if not file then ngx.say("can not open file") end local content = file:read('*all') file:close() -- ngx.say(content) -- json格式转化 local json = require("cjson") local ids = json.decode(content) local tc = 0 for i, v in pairs(ids.id) do if m[0] == v then tc = 1 end end --指向不同location if tc == 1 then ngx.exec("@bar") else ngx.exec("@two") end } } location @bar { # MIME type determined by default_type: default_type 'text/plain'; content_by_lua_block { ngx.say('Hello,world!') } } location @two { # MIME type determined by default_type: default_type 'text/plain'; content_by_lua_block { ngx.say('Hello,world two!') } } location /nginx_status { stub_status on; access_log off; } location ~ /.ht { deny all; } #access_log off; error_log /var/log/nginx/loc.lua_error.log; access_log /var/log/nginx/loc.lua_access.log; }
这篇关于nginx lua 根据参数指向不同location的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-12-13用Nginx防范DDoS攻击的那些事儿
- 2024-12-13用Terraform在AWS上搭建简单NGINX服务器指南
- 2024-10-29Nginx发布学习:从入门到实践的简单教程
- 2024-10-28Nginx发布:新手入门教程
- 2024-10-21nginx 怎么设置文件上传最大20M限制-icode9专业技术文章分享
- 2024-10-17关闭 nginx的命令是什么?-icode9专业技术文章分享
- 2024-09-17Nginx实用篇:实现负载均衡、限流与动静分离
- 2024-08-21宝塔nginx新增8022端口方法步骤-icode9专业技术文章分享
- 2024-08-21nginx配置,让ws升级为wss访问的方法步骤-icode9专业技术文章分享
- 2024-08-15nginx ws代理配置方法步骤-icode9专业技术文章分享