<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>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                ## 概述 Nginx作為一個成熟、久經考驗的負載均衡軟件,與其提供豐富、完整的內置變量是分不開的,它極大增加了對Nginx網絡行為的控制細度。這些變量大部分都是在請求進入時解析的,并把他們緩存到請求cycle中,方便下一次獲取使用。首先來看看Nginx對都開放了那些API。 > 內置變量列表 | 名稱 | 說明 | | --- | --- | | $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,帶參數 | | $scheme | 所用的協議,比如http或者是https | | $server\_addr | 服務器地址,如果沒有用listen指明服務器地址,使用這個變量將發起一次系統調用以取得地址(造成資源浪費) | | $server\_name | 請求到達的服務器名 | | $server\_port | 請求到達的服務器端口號 | | $server\_protocol | 請求的協議版本,"HTTP/1.0"或"HTTP/1.1" | | $uri | 請求的URI,可能和最初的值有不同,比如經過重定向之類的 | 其實這還不是全部,Nginx在不停迭代更新是一個原因,還有一個是有些變量太冷門,借助它們,會有很多玩法。 ## 使用 首先,在OpenResty中如何引用這些變量呢?參考[ngx.var.VARIABLE](https://github.com/openresty/lua-nginx-module#ngxvarvariable)小節。 利用這些內置變量,來做一個簡單的數學求和運算例子: ``` server { listen 80; server_name openresty.tinywan.com; location /resty_sum { content_by_lua_block { local a = tonumber(ngx.var.arg_a) or 0 local b = tonumber(ngx.var.arg_b) or 0 ngx.say("[x] sum: ", a + b ) } } } ``` 驗證結果 ``` PS C:\Users\Tinywan> curl -i "http://openresty.tinywan.com/resty_sum?a=2024&b=24" HTTP/1.1 200 OK Server: openresty/1.17.8.2 Date: Mon, 22 Jul 2024 07:28:47 GMT Content-Type: application/octet-stream Transfer-Encoding: chunked Connection: keep-alive [x] sum: 2048 ``` 也許你笑了,這個API太簡單沒有實際意義。我們做個簡易防火墻,看看如何開始玩耍。 參看下面示例代碼: ``` server { listen 80; server_name openresty.tinywan.com; location /resty_access_sum { # 使用access階段完成準入階段處理 access_by_lua_block { local black_ips = {["172.18.0.1"]=true} local ip = ngx.var.remote_addr if true == black_ips[ip] then ngx.exit(ngx.HTTP_FORBIDDEN) end } #處理業務 content_by_lua_block { local a = tonumber(ngx.var.arg_a) or 0 local b = tonumber(ngx.var.arg_b) or 0 ngx.say("[x] sum:", a + b ) } } } ``` 運行測試 ``` PS C:\Users\Tinywan> curl -i "http://openresty.tinywan.com/resty_access_sum?a=2024&b=24" HTTP/1.1 403 Forbidden Server: openresty/1.17.8.2 Date: Mon, 22 Jul 2024 08:42:30 GMT Content-Type: text/html; charset=utf-8 Content-Length: 159 Connection: keep-alive <html> <head><title>403 Forbidden</title></head> <body> <center><h1>403 Forbidden</h1></center> <hr><center>openresty/1.17.8.2</center> </body> </html> ``` 或者自定義錯誤提示語 ``` location /resty_access_sum { # 使用access階段完成準入階段處理 access_by_lua_block { local black_ips = {["172.18.0.1"]=true} local ip = ngx.var.remote_addr if true == black_ips[ip] then ngx.say("<html><body>Sorry, 403 Forbidden. 開源技術小棧.</body></html>") ngx.exit(ngx.HTTP_FORBIDDEN) end } } ``` 請求測試 ``` PS C:\Users\Tinywan> curl -i "http://openresty.tinywan.com/resty_access_sum?a=2024&b=24" HTTP/1.1 200 OK Server: openresty/1.17.8.2 Date: Mon, 22 Jul 2024 15:25:42 GMT Content-Type: application/octet-stream Transfer-Encoding: chunked Connection: keep-alive <html><body>Sorry, 403 Forbidden. 開源技術小棧.</body></html> ``` 通過測試結果看到,提取了終端的IP地址后進行限制。擴充一下,就可以支持 IP 地址段,如果再與系統`iptables`進行配合,那么就足以達到軟防火墻的目的。 目前為止,所有的例子都是對Nginx內置變量的獲取,是否可以對其進行設置呢?其實大多數內容都是不允許寫入的,例如剛剛的終端IP地址,在請求中是不允許對其進行更新的。對于可寫的變量中的`limit_rate`,值得一提,它能完成傳輸速率限制,并且它的影響是單個請求級別。 參看下面示例: ``` location /resty_download { # 使用access階段完成準入階段處理 access_by_lua_block { ngx.var.limit_rate = 1000 }; } ``` 下載測試: ``` wget "http://openresty.tinywan.com/resty_download/tinywan.lua" --2024-07-22 19:59:51-- http://openresty.tinywan.com/resty_download/tinywan.lua Connecting to 127.0.0.1:8866... connected. HTTP request sent, awaiting response... 200 OK Length: 135802 (133K) [application/octet-stream] Saving to: 'tinywan.lua' 1.cab 6%[===> ] 8.00K 1.01KB/s eta 1m 53s ```
                  <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>

                              哎呀哎呀视频在线观看