NGINX + LUA实现复杂的控制_牛刀杀鸡的博客-CSDN博客_nginx lua 修改url


本站和网页 https://blog.csdn.net/hh544/article/details/42592505 的作者无关,不对其内容负责。快照谨为网络故障时之索引,不代表被搜索网站的即时页面。

NGINX + LUA实现复杂的控制_牛刀杀鸡的博客-CSDN博客_nginx lua 修改url
NGINX + LUA实现复杂的控制
牛刀杀鸡
于 2015-01-10 23:33:27 发布
3906
收藏
分类专栏:
系统运维
Nginx
文章标签:
nginx
lua
系统运维
同时被 2 个专栏收录
14 篇文章
0 订阅
订阅专栏
Nginx
4 篇文章
0 订阅
订阅专栏
转自http://outofmemory.cn/code-snippet/14396/nginx-and-lua
lua_nginx_module 可以一步步的安装,也可以直接用淘宝的OpenResty
Centos和debian的安装就简单了。。
这里说下freebsd的安装:
fetch http://www.lua.org/ftp/lua-5.1.4.tar.gz
tar zxvf lua-5.1.4.tar.gz
cd lua-5.1.4
make freebsd
make install
cd ..
fetch https://github.com/chaoslawful/lua-nginx-module/zipball/v0.1.6rc2
fetch https://github.com/simpl/ngx_devel_kit/zipball/v0.2.17rc2
tar zxvf v0.1.6rc2
mv chaoslawful-lua-nginx-module-ccaf132 lua_nginx_module
tar zxvf v0.2.17rc2
mv simpl-ngx_devel_kit-bc97eea ngx_devel_kit
tar zxvf pcre-8.12.tar.gz
tar zxvf nginx-1.0.3.tar.gz
cd nginx-1.0.3
./configure --prefix=/data/soft/nginx --with-pcre=../pcre-8.12 --add-module=../ngx_devel_kit --add-module=../lua_nginx_module
make && make install
安装完成后,我们体验一下lua
第一个lua脚本
ngx.say 是打印的打印输出的意思。。。
location /echo {
default_type text/plain;
echo hello lua;
location /lua {
default_type text/plain;
content_by_lua 'ngx.say("hello world")';
用lua脚本做nginx的访问的限制...
location @client{
proxy_pass http://www.ruifengyun.com;
location ~ /test {
default_type text/html;
content_by_lua 'ngx.say("this is ruifengyun.com!")';
access_by_lua '
if ngx.var.remote_addr == "10.2.20.110" then
ngx.exit(ngx.HTTP_FORBIDDEN)
end
if ngx.var.remote_addr == "10.2.20.112" then
ngx.exec("@client")
end
';
控制经过判断之后,才能访问
location / {
access_by_lua '
local res = ngx.location.capture("/auth")
if res.status == ngx.HTTP_OK then
return
end
if res.status == ngx.HTTP_FORBIDDEN then
ngx.exit(res.status)
end
ngx.exit(ngx.HTTP_INTERNAL_SERVER_ERROR)
';
# proxy_pass/fastcgi_pass/postgres_pass/...
使用lua做nginx的rewrite跳转
这个是先判断 check-pam接口的return的内容是不是spam,是的话,转跳到其他的页面
location / {
rewrite_by_lua '
local res = ngx.location.capture("/check-spam")
if res.body == "spam" then
ngx.redirect("/terms-of-use.html")
end
'; fastcgi_pass ...;
根据ip做不同的响应
location / {
content_by_lua '
myIP = ngx.req.get_headers()["X-Real-IP"]
if myIP == nil then
myIP = ngx.req.get_headers()["x_forwarded_for"]
end
if myIP == nil then
myIP = ngx.var.remote_addr
end
if myIP == "" then
ngx.exec("@client")
else
ngx.exec("@client_test")
end
';
redirect的使用
return ngx.redirect("/foo")
return ngx.redirect("http://localhost:1984/foo", ngx.HTTP_MOVED_TEMPORARILY)
return ngx.redirect("/foo", 301)
返回302临时重定向 地址栏会显示跳转后的地址
rewrite ^ /foo? redirect; # nginx config
return ngx.redirect('/foo'); -- lua code
lua过滤post过来的参数
location = /test {
content_by_lua '
ngx.req.read_body()
local args = ngx.req.get_post_args()
for key, val in pairs(args) do
if type(val) == "table" then
ngx.say(key, ": ", table.concat(val, ", "))
else
ngx.say(key, ": ", val)
end
end
';
一个Lua的例子:
#!/usr/bin/env lua
ngx.say('aaaaaa </br>')
local url = ngx.var.uri
ngx.say('<br>',url,'
')
ngx.print('这次访问的header头是 ',ngx.req.raw_header())
ngx.print('<meta http-equiv="content-type" content="text/html;charset=utf-8">')
ngx.print('<h1> 这个是 h1 </h1>')
ngx.print('这次访问的是 get 还是 post 呀 ',ngx.req.get_Method())
local args = ngx.req.get_uri_args()
ngx.print(args)
local res = ngx.location.capture("/")
ngx.print('<br>http code <br>‘,res.status)
lua 调用mysql的例子
worker_processes 2;
error_log logs/error.log warn;
events {
worker_connections 1024;
http {
upstream backend {
drizzle_server 127.0.0.1:3306 protocol=mysql
dbname=ngx_test user=ngx_test password=ngx_test;
drizzle_keepalive max=10 overflow=ignore mode=single;
server {
listen 8080;
location @cats-by-name {
set_unescape_uri $name $arg_name;
set_quote_sql_str $name;
drizzle_query 'select * from cats where name=$name';
drizzle_pass backend;
rds_json on;
location @cats-by-id {
set_quote_sql_str $id $arg_id;
drizzle_query 'select * from cats where id=$id';
drizzle_pass backend;
rds_json on;
location = /cats {
access_by_lua '
if ngx.var.arg_name then
return ngx.exec("@cats-by-name")
end
if ngx.var.arg_id then
return ngx.exec("@cats-by-id")
end
';
rds_json_ret 400 "expecting \"name\" or \"id\" query arguments";
改改密码就能用啦~
lua获取url中的参数
location = /adder {
set_by_lua $res "
local a = tonumber(ngx.arg[1])
local b = tonumber(ngx.arg[2])
return a + b" $arg_a $arg_b;
echo $res;
ngx.req.set_uri
nginx里面的配置是:
location /test {
rewrite ^/test/(.*) /$1 break;
proxy_pass http://my_backend;
lua里面的配置是:
location /test {
rewrite_by_lua '
local uri = ngx.re.sub(ngx.var.uri, "^/test/(.*)", "$1", "o")
ngx.req.set_uri(uri)
';
proxy_pass http://my_backend;
我想大家看这个对照,已经知道是啥意思了.
通过lua获取nginx的内置变量,通过这些变量做些逻辑的处理~
Nginx提供了很多内置的变量,如:
$arg_PARAMETER 这个变量包含在查询字符串时GET请求PARAMETER的值。$args 这个变量等于请求行中的参数。$binary_remote_addr 二进制码形式的客户端地址。$body_bytes_sent 传送页面的字节数$content_length 请求头中的Content-length字段。$content_type 请求头中的Content-Type字段。$cookie_COOKIE cookie COOKIE的值。$document_root 当前请求在root指令中指定的值。$document_uri 与$uri相同。$host 请求中的主机头字段,如果请求中的主机头不可用,则为服务器处理请求的服务器名称。$is_args 如果$args设置,值为"?",否则为""。$limit_rate 这个变量可以限制连接速率。$nginx_version 当前运行的nginx版本号。$query_string 与$args相同。$remote_addr 客户端的IP地址。$remote_port 客户端的端口。$remote_user 已经经过Auth Basic Module验证的用户名。$request_filename 当前连接请求的文件路径,由root或alias指令与URI请求生成。$request_body 这个变量(0.7.58+)包含请求的主要信息。在使用proxy_pass或fastcgi_pass指令的location中比较有意义。$request_body_file 客户端请求主体信息的临时文件名。$request_completion 未知。$request_method 这个变量是客户端请求的动作,通常为GET或POST。包括0.8.20及之前的版本中,这个变量总为main request中的动作,如果当前请求是一个子请求,并不使用这个当前请求的动作。$request_uri 这个变量等于包含一些客户端请求参数的原始URI,它无法修改,请查看$uri更改或重写URI。$scheme 所用的协议,比如http或者是https,比如rewrite ^(.+)$ $scheme://example.com$1 redirect;$server_addr 服务器地址,在完成一次系统调用后可以确定这个值,如果要绕开系统调用,则必须在listen中指定地址并且使用bind参数。$server_name 服务器名称。$server_port 请求到达服务器的端口号。$server_protocol 请求使用的协议,通常是HTTP/1.0或HTTP/1.1。$uri 请求中的当前URI(不带请求参数,参数位于$args),可以不同于浏览器传递的$request_uri的值,它可以通过内部重定向,或者使用index指令进行修改。
另外: HTTP_X_FORWARDED_FOR是透过代理服务器取得客户端的真实IP地址,有些用此方法读取到的仍然是代理服务器的IP。还有一点需要注意的是:如果客户端没有通过代理服务器来访问,那么用 HTTP_X_FORWARDED_FOR 取到的值将是空的。
函数版的访问
location /lua1 {
default_type 'text/plain';
content_by_lua 'ngx.say("hello, lua")';
# 请求另外的url
location /lua2 {
content_by_lua '
  local res = ngx.location.capture("/hello1")
  ngx.say("data: " .. res.body)
';
牛刀杀鸡
关注
关注
点赞
收藏
评论
NGINX + LUA实现复杂的控制
转自http://outofmemory.cn/code-snippet/14396/nginx-and-lualua_nginx_module 可以一步步的安装,也可以直接用淘宝的OpenRestyCentos和debian的安装就简单了。。这里说下freebsd的安装:fetch http://www.lua.org/ftp/lua-5.1.4.tar.gztar
复制链接
扫一扫
专栏目录
openresty使用lua拦截proxy_pass重定向301 302
blue_sky的博客
04-23
3885
前言:
在使用nginx proxy_pass后端资源时候,有时候后端会返回301、302、304重定向,这样导致用户等浏览器直接访问来最终的等资源,无法经过nginx proxy_pass,这种时候nginx代理就没有意义了。openresty集成了lua,通过lua可以在nginx返回客户端的时候,修改包头,改写重定向规则,即可处理重定向问题
302示例,访问资源一个资源:正常ngin...
nginx使用lua文件动态代理websocket
无痕的博客
08-30
1187
创建的websocket服务,可以直接使用http://192.168.11.199:21899/访问,如下
但现在不想对外暴露websocket的端口号,通过nginx反向代理:待访问的url为http://192.168.11.199:81/?destip=192.168.11.199:21899 destip参数即为websocket服务的ip与port
一、第一次的nginx.conf配置如下
1.1、nginx.conf
location / {
...
参与评论
您还未登录,请先
登录
后发表或查看评论
lua-nginx-module
最新发布
weixin_43961989的博客
10-18
144
ngx_http_lua_module - Embed the power of Lua into Nginx HTTP Servers.This module is a core component of OpenResty. If you are using this module,
then you are essentially using OpenResty.This module is not distributed with the Nginx source. See
the installa
利用lua的类似于replace替换nginx的请求url地址,任何场景都行,我这里主要是为了中文直接传入不被转码
sinat_39327967的博客
04-25
1447
nginx利用lua 进行地址替换 类似于replace
Nginx+Lua实现POST参数篡改并代理到第三方
zongyue_wang的博客
09-20
3754
Nginx+Lua实现POST参数篡改并代理到第三方
vhost_x.conf
server {
listen 80;
server_name proxy-wechat.gitv.we;
lua_code_cache on;
access_log logs/proxy-wechat.gitv.we.access.log gitv;
error_log logs/proxy-wecha...
Nginx+lua通过url传参的方式实现动态代理
07-20
需求大致如下:通过url传参的方式,让Nginx代理到不同的服务器
浏览器输入:http://127.0.0.1/remote?port=8081被代理到:http://192.168.108.2:8081
nginx-- 利用set_by_lua进行优化
xiaoxiaobian3310903的专栏
09-03
6172
背景
服务是采用nginx+lua实现的,在nginx的配置中存在多处(共20多处)正则判断,期望进行优化
location ~* /test.gif {
rewrite_by_lua_file 'lua/edit.lua';
if ($query_string ~* "pd=8(&|$)") {
set $req_host inner.a.com;
...
nginx 用lua中获取url路径相关信息
热门推荐
xiejunna的博客
05-11
2万+
1.获取当前请求的url相关信息function test()
-- 这个变量等于包含一些客户端请求参数的原始URI,它无法修改,请查看$uri更改或重写URI。
local request_uri = ngx.var.request_uri
log(tools.gbk_to_u8("获取当前请求的url==") .. tools.u8_to_gbk(cjson.encode(request_ur
nginx+lua 根据指定路径反向代理
weixin_30952535的博客
03-26
306
1 location /imgproxytest{
2 if ($uri ~ ".*\.(jpg|png|jpeg|bmp|gif|swf|css)$"){
3 rewrite_by_lua 'local url = ngx.var.request_uri local new_url = string.gsub(url,"/imgproxytest","") ngx.redirect...
nginx+lua脚本实现url参数动态请求服务--jerry出品
jerry的博客
05-24
1268
#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;
defaul...
nginx使用lua基于url参数分发不同文件对比测试
weixin_33858249的博客
01-22
108
lua-nginx-module
get_system_live_nodes_1, get_system_live_nodes_2和get_system_live_nodes_3都在/disk2/dev_lookup/目录中,且大小完全相同阿里ECS局域网测试nginx server: 4核8Gab: 8核8G
使用ngx.location.capture
server {
listen ...
在nginx中采用lua对请求的url进行hash取模
唯一的专栏
03-30
7335
目前有一个需求,是需要将nginx反向代理的后端响应的内容进行缓存,并且要平均的分配到10个自定义的目录中。一说到,自动分配的需求,采用hash取模是最常用也是最简单的方案,其中选择hash的字段(内容)必须是变化最多的。比如请求头里面的host和request_url,肯定选择是request_url,因为其变化的内容比较大。
查看了nginx的官网文档也没有单独对url进行取模的功能
用Lua控制Nginx静态文件的url访问权限
猿起于此
09-07
348
参考地址: https://www.cnblogs.com/gouyg/p/lua-nginx-url-access.html
nginx lua打印url、headers和body
依步_的专栏
10-19
1450
server {
listen 1234;
location / {
content_by_lua '
ngx.req.read_body();
local data = ngx.req.get_headers();
local xx = "";
for k, v in pairs(data) do
...
nginx配置,配合lua编程实现
weixin_30616969的博客
02-22
97
本次目的是使用nginx结合lua语言配置待检测功能的服务器
首先nginx配置项:
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid ...
LUA--AWS分布式代理利用NGINX替换AWS请求头
weixin_44148981的博客
06-18
357
下面标注一轮都说明一个问题:想要替换sign,替换整个 auth 头字串就ok———authorization_header
1/标准请求头
Authorization: AWS4-HMAC-SHA256 Credential=XXXXXXXXXXXX/20170522/us-east-1/s3/aws4_request,SignedHeaders=content-md5;
host;
x-amz-content-sha256;
x-amz-date;
x-amz-decoded-content-lengt
java 权限url权限_通过lua进行nginx的权限控制
weixin_39885067的博客
11-20
94
nginx_lua的安装nginx使用luajit进行编译安装使用openresty进行yum安装openresty中将lua和nginx进行封装,详情可查看openresty官网openresty相关启动命令service openresty startngx_lua的相关api使用说明及相关使用ngx_lua github地址lua基本语法openrestry最佳实践ngx_lua的日常使用场...
“相关推荐”对你有帮助么?
非常没帮助
没帮助
一般
有帮助
非常有帮助
提交
©️2022 CSDN
皮肤主题:编程工作室
设计师:CSDN官方博客
返回首页
牛刀杀鸡
CSDN认证博客专家
CSDN认证企业博客
码龄13年
暂无认证
42
原创
22万+
周排名
57万+
总排名
11万+
访问
等级
1271
积分
粉丝
获赞
评论
13
收藏
私信
关注
热门文章
netty源码构建找不到netty-tcnative包 --- Maven <classifier>元素
20109
Intellij Idea中运行tomcat 报內存溢出 PermGen space 解决方案
13471
nginx端口转发tomcat,80转8080和8433
5703
netty github 导入 Eclipse
4947
NGINX + LUA实现复杂的控制
3906
分类专栏
C/C++
6篇
Java
33篇
Java NIO/Netty
4篇
JVM
1篇
Python
3篇
Ruby
Maven
7篇
Nginx
4篇
Unix
8篇
Mysql
1篇
系统运维
14篇
架构
7篇
杂谈
3篇
shell
1篇
web
1篇
android
1篇
akka
1篇
最新评论
netty源码构建找不到netty-tcnative包 --- Maven <classifier>元素
lijie12233
回复
homeless_boy:
为什么我去掉还是不能编译
netty源码构建找不到netty-tcnative包 --- Maven <classifier>元素
homeless_boy:
<classifier>${os.detected.classifier}</classifier>
把这个改成
<classifier></classifier>
系统去找包的时候就去找的netty-tcnative-1.1.32.Fork1.jar这样就有了
netty源码构建找不到netty-tcnative包 --- Maven <classifier>元素
Givemefive555
回复
牛刀杀鸡:
恩 改起来很费劲 请问你是怎么改的 我把我的maven依赖直接改的名字。。。改成_x86_32
netty源码构建找不到netty-tcnative包 --- Maven <classifier>元素
牛刀杀鸡
回复
Givemefive555:
netty多个子项目里都有os.detected.classifier ,需要逐个检查,属于maven项目管理方面的东西
您愿意向朋友推荐“博客详情页”吗?
强烈不推荐
不推荐
一般般
推荐
强烈推荐
提交
最新文章
关于java io 的 write 与 操作系统
三个Java多线程循环顺序周期打印1-75个数
Python和数据科学的起步指南
2017年2篇
2016年7篇
2015年60篇
目录
目录
分类专栏
C/C++
6篇
Java
33篇
Java NIO/Netty
4篇
JVM
1篇
Python
3篇
Ruby
Maven
7篇
Nginx
4篇
Unix
8篇
Mysql
1篇
系统运维
14篇
架构
7篇
杂谈
3篇
shell
1篇
web
1篇
android
1篇
akka
1篇
目录
评论
被折叠的 条评论
为什么被折叠?
到【灌水乐园】发言
查看更多评论
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额
抵扣说明:
1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。 2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。
余额充值