<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # Nginx Lua API 和一般的Web Server類似,我們需要接收請求、處理并輸出響應。而對于請求我們需要獲取如請求參數、請求頭、Body體等信息;而對于處理就是調用相應的Lua代碼即可;輸出響應需要進行響應狀態碼、響應頭和響應內容體的輸出。因此我們從如上幾個點出發即可。 ## 接收請求 #### 1. openResty.conf配置文件 ``` server { listen 80; server_name _; location ~ /lua_request/(\d+)/(\d+) { # 設置nginx變量 set $a $1; set $b $host; default_type 'text/html'; lua_code_cache off; # nginx內容處理 # content_by_lua_file /usr/openResty/lua/test.lua; content_by_lua_file /usr/example/lua/test_request.lua; # 內容體處理完成后調用 echo_after_body "ngx.var.b $b"; } } ``` #### 2. test_request.lua ``` -- nginx變量 local var = ngx.var ngx.say("ngx.var.a : ", var.a, "<br/>") ngx.say("ngx.var.b : ", var.b, "<br/>") ngx.say("ngx.var[2] : ", var[2], "<br/>") ngx.var.b = 2; ngx.say("<br/>") -- 請求頭 local headers = ngx.req.get_headers() ngx.say("headers begin", "<br/>") ngx.say("Host : ", headers["Host"], "<br/>") ngx.say("user-agent : ", headers["user-agent"], "<br/>") ngx.say("user-agent : ", headers.user_agent, "<br/>") for k, v in pairs(headers) do if type(v) == "table" then ngx.say(k, ":", table.concat(v, ","), "<br/>") else ngx.say(k, " : ", v, "<br/>") end end ngx.say("headers end", "<br/>") ngx.say("<br/>") -- get請求uri參數 ngx.say("uri args begin", "<br/>") local uri_args = ngx.req.get_uri_args() for k, v in pairs(uri_args) do if type(v) == "table" then ngx.say(k, " : ", table.concat(v, ", "), "<br/>") else ngx.say(k, ": ", v, "<br/>") end end ngx.say("uri args end", "<br/>") ngx.say("<br/>") -- post請求參數 ngx.req.read_body() ngx.say("post args begin", "<br/>") local post_args = ngx.req.get_post_args() for k, v in pairs(post_args) do if type(v) == "table" then ngx.say(k, ":", table.concat(v, ", "), "<br/>") else ngx.say(k, ": ", v, "<br/>") end end ngx.say("post args end", "<br/>") ngx.say("<br/>") -- 請求的http協議版本 ngx.say("ngx.req.http_version:", ngx.req.http_version(), "<br/>") -- 請求方法 ngx.say("ngx.req.get_method:", ngx.req.get_method(), "<br/>") -- 原始的請求頭內容 ngx.say("ngx.req.raw_header:", ngx.req.raw_header(), "<br/>") -- 請求的body內容體 ngx.say("ngx.req.get_body_data():", ngx.req.get_body_data(), "<br/>") ngx.say("<br/>") ``` **ngx.var**?: nginx變量,如果要賦值如ngx.var.b = 2,此變量必須提前聲明;另外對于nginx location中使用正則捕獲的捕獲組可以使用ngx.var\[捕獲組數字\]獲取; **ngx.req.get\_headers**:獲取請求頭,默認只獲取前100,如果想要獲取所以可以調用ngx.req.get\_headers(0);獲取帶中劃線的請求頭時請使用如headers.user\_agent這種方式;如果一個請求頭有多個值,則返回的是table; **ngx.req.get\_uri\_args**:獲取url請求參數,其用法和get\_headers類似; **ngx.req.get\_post\_args**:獲取post請求內容體,其用法和get\_headers類似,但是必須提前調用**ngx.req.read\_body()**:來讀取body體(也可以選擇在nginx配置文件使用lua\_need\_request\_body on;開啟讀取body體,但是官方不推薦); **ngx.req.raw\_header**:未解析的請求頭字符串; **ngx.req.get\_body\_data**:為解析的請求body體內容字符串。 <br/> 如上方法處理一般的請求基本夠用了。另外在讀取post內容體時根據實際情況設置[client\_body\_buffer\_size](http://wiki.nginx.org/HttpCoreModule#client_body_buffer_size "HttpCoreModule")和[client\_max\_body\_size](http://wiki.nginx.org/HttpCoreModule#client_max_body_size "HttpCoreModule")來保證內容在內存而不是在文件中。 <br/> 使用如下腳本測試 ``` wget?--post-data?'a=1&b=2' 'http://127.0.0.1/lua_request/1/2?a=3&b=4'?-O?- ``` 結果如下所示: ![](https://box.kancloud.cn/37760d21fc120b3a1b5372fdfddc65f3_1698x856.png) ## 輸出響應 #### 1. openResty.conf配置文件增加配置 ``` location /lua_response_1 { default_type "text/html"; content_by_lua_file /usr/openResty/lua/test_response_1.lua; } ``` #### 2. test_response_1.lua ``` -- 寫響應頭 ngx.header.a = "1" -- 多個響應頭可用table ngx.header.b = {"2", "3"} -- 輸出響應 ngx.say("a", "b", "<br/>") ngx.print("c", "d", "<br/>") -- 200狀態碼退出 return ngx.exit(200) ``` **ngx.header**:輸出響應頭; **ngx.print**:輸出響應內容體; **ngx.say**:通ngx.print,但是會最后輸出一個換行符; **ngx.exit**:指定狀態碼退出。 使用postman測試結果如下 ![](https://box.kancloud.cn/05f51d7d6a32031f9a8ac12bfaec1c11_598x468.png) ![](https://box.kancloud.cn/ad723cde217fc7270b79728e7cf74aed_602x484.png) ## 重定向 #### 1. openResty.conf配置文件增加配置 ``` location /lua_response_2 { default_type "text/html"; content_by_lua_file /usr/openResty/lua/test_response_2.lua; } ``` #### 2. test_response_2.lua ``` ngx.redirect("https://www.baidu.com", 302) ``` **ngx.redirect**:重定向 **ngx.status**:狀態碼,設置響應的狀態碼 **ngx.resp.get_headers()**:獲取設置的響應狀態碼 **ngx.send_headers()**:發送響應狀態碼,當調用ngx.say/ngx.print時自動發送響應狀態碼;可以通過 **ngx.headers_sent=true**:判斷是否發送了響應狀態碼 ## 其他API #### 1. openResty.conf配置文件增加配置 ``` location /lua_other { default_type "text/html"; content_by_lua_file /usr/openResty/lua/test_other.lua; } ``` #### 2. test_other.lua ``` -- 未經解碼的請求uri local request_uri = ngx.var.request_uri; ngx.say("request_uri : ", request_uri, "<br/>"); -- 解碼 ngx.say("decode request_uri : ", ngx.unescape_uri(request_uri), "<br/>"); -- MD5 ngx.say("ngx.md5 : ", ngx.md5("123"), "<br/>"); -- http time ngx.say("ngx.http_time : ", ngx.http_time(ngx.time()), "<br/>"); ``` **ngx.escape_uri/ngx.unescape_uri**?: uri編碼解碼; **ngx.encode_args/ngx.decode_args**:參數編碼解碼; **ngx.encode_base64/ngx.decode_base64**:BASE64編碼解碼; **ngx.re.match**:nginx正則表達式匹配; 更多Nginx Lua API請參考?[http://wiki.nginx.org/HttpLuaModule#Nginx_API_for_Lua](http://wiki.nginx.org/HttpLuaModule#Nginx_API_for_Lua) ## Nginx全局內存 使用過如Java的朋友可能知道如Ehcache等這種進程內本地緩存,Nginx是一個Master進程多個Worker進程的工作方式,因此我們可能需要在多個Worker進程中共享數據,那么此時就可以使用[ngx.shared.DICT](http://wiki.nginx.org/HttpLuaModule#ngx.shared.DICT.incr)來實現全局內存共享。 #### 1. 首先在nginx.conf的http部分分配內存大小 ``` #共享全局變量,在所有worker間共享 lua_shared_dict?shared_data?1m; ``` #### 2. openResty.conf配置文件增加配置 ``` location /lua_shared_dict { default_type "text/html"; content_by_lua_file /usr/openResty/lua/test_lua_shared_dict.lua; } ``` #### 3. test_lua_shared_dict.lua ``` -- 1. 獲取全局共享內存變量 local shared_data = ngx.shared.shared_data -- 2. 獲取字典值 local i = shared_data:get("i") if not i then i = 1 -- 3. 惰性賦值 shared_data:set("i", i) ngx.say("lazy set i ", i, "<br/>") end -- 遞增 i = shared_data:incr("i", i) ngx.say("i=", i, "<br/>") ``` 更多API請參考[http://wiki.nginx.org/HttpLuaModule#ngx.shared.DICT](http://wiki.nginx.org/HttpLuaModule#ngx.shared.DICT) 到此基本的Nginx Lua API就介紹完了,對于請求處理和輸出響應如上介紹的API完全夠用了,更多API請參考官方文檔。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看