openresty开发系列11--openresty的api入门
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";
?? ?}
簡單配置示例:
?
轉載于:https://www.cnblogs.com/reblue520/p/11429432.html
總結
以上是生活随笔為你收集整理的openresty开发系列11--openresty的api入门的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: openresty开发系列10--ope
- 下一篇: openresty开发系列12--lua