首页 技术 正文
技术 2022年11月15日
0 收藏 402 点赞 3,742 浏览 3202 个字

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  ;#pid        logs/nginx.pid;events {
worker_connections ;
}http {
include mime.types;
#default_type application/octet-stream;
default_type text/html; sendfile on; keepalive_timeout ; server {
listen ;
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 /50x.html;
location = /50x.html {
root html;
} }}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,121
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,593
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,438
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,209
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,845
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,930