openresty开发系列11--openresty的api入门

1)ngx_lua模块的hello world
编辑nginx下conf配置文件nginx.conf
# vi nginx.conf
在server模块加上
location /helloworld {
    default_type text/html;
    content_by_lua 'ngx.say("hello world")';
}

检查配置文件是否正确
# /usr/local/openresty/nginx/sbin/nginx -t -c /usr/local/openresty/nginx/conf/nginx.conf

重启nginx
# ./nginx -s reload
访问http://10.11.0.215/helloworld  输出 hello world

2)nginx的内部变量

名称 说明
$arg_name 请求中的name参数
$args 请求中的参数
$binary_remote_addr 远程地址的二进制表示
$body_bytes_sent 已发送的消息体字节数
$content_length HTTP请求信息里的"Content-Length"
$content_type 请求信息里的"Content-Type"
$document_root 针对当前请求的根路径设置值
$document_uri 与$uri相同; 比如 /test2/test.php
$host 请求信息中的"Host",如果请求中没有Host行,则等于设置的服务器名
$hostname 机器名使用 gethostname系统调用的值
$http_cookie cookie 信息
$http_referer 引用地址
$http_user_agent 客户端代理信息
$http_via 最后一个访问服务器的Ip地址。
$http_x_forwarded_for 相当于网络访问路径
$is_args 如果请求行带有参数,返回“?”,否则返回空字符串
$limit_rate 对连接速率的限制
$nginx_version 当前运行的nginx版本号
$pid worker进程的PID
$query_string 与$args相同
$realpath_root 按root指令或alias指令算出的当前请求的绝对路径。其中的符号链接都会解析成真是文件路径
$remote_addr 客户端IP地址
$remote_port 客户端端口号
$remote_user 客户端用户名,认证用
$request 用户请求
$request_body 这个变量(0.7.58+)包含请求的主要信息。在使用proxy_pass或fastcgi_pass指令的location中比较有意义
$request_body_file 客户端请求主体信息的临时文件名
$request_completion 如果请求成功,设为"OK";如果请求未完成或者不是一系列请求中最后一部分则设为空
$request_filename 当前请求的文件路径名,比如/opt/nginx/www/test.php
$request_method 请求的方法,比如"GET"、"POST"等
$request_uri 请求的URI,带参数; 比如http://localhost:88/test1/
$scheme 所用的协议,比如http或者是https
$server_addr 服务器地址,如果没有用listen指明服务器地址,使用这个变量将发起一次系统调用以取得地址(造成资源浪费)
$server_name 请求到达的服务器名
$server_port 请求到达的服务器端口号
$server_protocol 请求的协议版本,"HTTP/1.0"或"HTTP/1.1"
$uri 请求的URI,可能和最初的值有不同,比如经过重定向之类的

写个配置文件,测试一下$uri变量
location /test_url {
    echo "url:$uri";
}

location /test_url {
            echo "url:$uri";
            echo "full url : $host$request_uri";
        }
        
重启nginx,访问

再此基础上测试$args变量
location /test_url {
    echo "url:$uri----args:$args";
}

再此基础上测试$arg_name变量
location /test_url {
    echo "url:$uri----args:$args--------arg_name:$arg_name";
}

说明一下,$arg_name表示取名为name的参数,如果想取其他名称的参数可以对应的取该参数名
location /test_url {
    echo "url:$uri --- args:$args --- arg_name:$arg_name <br/>";
    echo "arg_user:$arg_user --- arg_age:$arg_age<br/>";
    echo "arg_test:$arg_test";
}

test参数名不存在,则为空。
自定义变量
location /test_def {
    set $name "rainbow";
    echo $name;
}

set 设置的变量 为局部变量,其他请求无法获取此$name的值
location /test_def {
    set $name "rainbow";
    echo_exec /test_def2;  ##内部跳转,可以传递变量
}
location /test_def2 {
    echo $name;
}

# 传递变量,访问http://www.server1.com/test_var1 可以打印cityname的值
location /test_var1{
        set $cityname "shenzhen";
        echo_exec /test_var2;
    }

location /test_var2{
        echo "test_var2=$cityname";
    }

简单配置示例:

[root@node5 conf]# cat /usr/local/openresty/nginx/conf/nginx.confworker_processes  4;#pid        logs/nginx.pid;events {worker_connections  10240;
}http {include       mime.types;#default_type  application/octet-stream;default_type  text/html;sendfile        on;keepalive_timeout  65;server {listen       80;server_name  www.server1.com;location /lua {content_by_lua 'ngx.say("hello world")';}location /test_url {echo "url:$uri";echo "full url : $host$request_uri";echo "url:$uri --- args:$args";echo "args_username:$arg_username";set $name "jackson";echo "$name";}location /test_url2{echo $name;}location /test_var1{set $cityname "shenzhen";echo_exec /test_var2;}location /test_var2{echo "test_var2=$cityname";}location / {root   html;index  index.html index.htm;}error_page   500 502 503 504  /50x.html;location = /50x.html {root   html;}}}

转载于:https://www.cnblogs.com/reblue520/p/11429432.html

openresty开发系列11--openresty的api入门相关推荐

  1. openresty开发系列40--nginx+lua实现获取客户端ip所在的国家信息

    openresty开发系列40--nginx+lua实现获取客户端ip所在的国家信息 为了实现业务系统针对不同地区IP访问,展示包含不同地区信息的业务交互界面.很多情况下系统需要根据用户访问的IP信息 ...

  2. openresty开发系列37--nginx-lua-redis实现访问频率控制

    openresty开发系列37--nginx-lua-redis实现访问频率控制 一)需求背景 在高并发场景下为了防止某个访问ip访问的频率过高,有时候会需要控制用户的访问频次 在openresty中 ...

  3. openresty开发系列33--openresty执行流程之3重写rewrite和重定向

    openresty开发系列33--openresty执行流程之3重写rewrite和重定向 重写rewrite阶段 1)重定向 2)内部,伪静态 先介绍一下if,rewrite指令 一)if指令 语法 ...

  4. openresty开发系列33--openresty执行流程之2重写赋值阶段

    openresty开发系列33--openresty执行流程之2重写赋值阶段 一)重写赋值阶段 1)set_by_lua 语法:set_by_lua $res <lua-script-str&g ...

  5. openresty开发系列28--openresty中操作mysql

    openresty开发系列28--openresty中操作mysql Mysql客户端    应用中最常使用的就是数据库了,尤其mysql数据库,那openresty lua如何操作mysql呢?   ...

  6. openresty开发系列26--openresty中使用redis模块

    openresty开发系列26--openresty中使用redis模块 在一些高并发的场景中,我们常常会用到缓存技术,现在我们常用的分布式缓存redis是最知名的, 操作redis,我们需要引入re ...

  7. openresty开发系列24--openresty中lua的引入及使用

    openresty开发系列24--openresty中lua的引入及使用 openresty 引入 lua 一)openresty中nginx引入lua方式 1)xxx_by_lua   ---> ...

  8. openresty开发系列38--通过Lua+Redis 实现动态封禁IP

    openresty开发系列38--通过Lua+Redis 实现动态封禁IP 一)需求背景为了封禁某些爬虫或者恶意用户对服务器的请求,我们需要建立一个动态的 IP 黑名单.对于黑名单之内的 IP ,拒绝 ...

  9. openresty开发系列36--openresty执行流程之6日志模块处理阶段

    openresty开发系列36--openresty执行流程之6日志模块处理阶段 一)header_filter_by_lua 语法:header_filter_by_lua <lua-scri ...

最新文章

  1. python中一共有多少个关键字-Python之33个关键字是哪些
  2. BFS——广度优先算法(Breadth First Search)
  3. 初识MongoDB(四)
  4. 返回一个不确定的对象_我有一个Android必备知识点,你确定不了解一下?
  5. [leetcode] 1335. 工作计划的最低难度
  6. [JS6] 通过用户事件事件执行脚本
  7. android心率曲线绘制,巧妙绘制心率曲线图的方法实践
  8. 破解jQuery插件收费、下载币(单页扒站小工具)
  9. java如何设置控制台打印的字体颜色、背景、字体样式(idea设置打印字体样式)工具类 - 附插件方式
  10. 没学历没经验没外语没技能,啥都没有靠啥找工作?
  11. java 且 或 符号_Java 、、|、||、^、、、~、等运算符
  12. 1U,2U,3U,4U机箱面板,U的含义
  13. Error:1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL
  14. 计算机文本使用技巧,技巧:“文本恢复转换器”随附WORD的使用_计算机_IT /计算机_数据的基本知识...
  15. springboot 集成 RabbitMQ confirm 确认模式和 return 回退模式以及Consumer Ack模式
  16. CAD二次开发--三维多段线偏移(Polyline3d offset)实现方法【GetOffsetCurves附带原代码】
  17. 知乎热议: Java, Go和Python那个前景好?
  18. 5.1 node实现简单注册功能(往json文件中写入数据)
  19. python夯实基础日记-for循环、优化技巧、函数
  20. 嵌入式linux矩阵键盘,基于嵌入式Linux的矩阵键盘驱动程序开发

热门文章

  1. python中dir用法_Python内置函数dir详解
  2. CSDN登录机制分析(附上python、java代码)
  3. C语言实例第6期:反转字符串
  4. Java 利用反射实现C#的委托
  5. 计算机组装时遇到的问题,计算机组装及正常维修过程中遇到的问题和解决方法.doc...
  6. Linux 服务器性能参数指标总结
  7. Spring Boot Admin 集成诊断利器 Arthas 实践
  8. 国货之光业务增长背后的技术支持 - 完美日记的云原生实践
  9. 把阿里巴巴的核心系统搬到云上,架构上的挑战与演进是什么?
  10. php将img中的宽高删除,PHP删除HTML中宽高样式的详解