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

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                [TOC] ## 概述 OpenResty?是一個基于[Nginx](https://openresty.org/cn/nginx.html "Nginx")與 Lua 的高性能 Web 平臺,其內部集成了大量精良的 Lua 庫、第三方模塊以及大多數的依賴項。用于方便地搭建能夠處理超高并發、擴展性極高的動態 Web 應用、Web 服務和動態網關。 OpenResty?通過匯聚各種設計精良的[Nginx](https://openresty.org/cn/nginx.html "Nginx")模塊(主要由 OpenResty 團隊自主開發),從而將[Nginx](https://openresty.org/cn/nginx.html "Nginx")有效地變成一個強大的通用 Web 應用平臺。這樣,Web 開發人員和系統工程師可以使用 Lua 腳本語言調動[Nginx](https://openresty.org/cn/nginx.html "Nginx")支持的各種 C 以及 Lua 模塊,快速構造出足以勝任 10K 乃至 1000K 以上單機并發連接的高性能 Web 應用系統。 OpenResty?的目標是讓你的Web服務直接跑在[Nginx](https://openresty.org/cn/nginx.html "Nginx")服務內部,充分利用[Nginx](https://openresty.org/cn/nginx.html "Nginx")的非阻塞 I/O 模型,不僅僅對 HTTP 客戶端請求,甚至于對遠程后端諸如 MySQL、PostgreSQL、Memcached 以及 Redis 等都進行一致的高性能響應。 ## 我的目的 項目中有個用到http請求,我用shell腳本+curl簡單實現了一下.但是我想用一個我不熟悉的語言來做該功能, 在網上搜索到可以通過lua來實現我的需求.. 于是我找到了OpenResty框架 . lua發送http請求一般有兩種方式, 使用?socket.http 或者?resty.http ## 環境&安裝 OpenResty 參考:https://openresty.org/cn/linux-packages.html 環境:Ubuntu 安裝 ``` $ sudo apt-get -y install --no-install-recommends wget gnupg ca-certificates $ echo "deb http://openresty.org/package/ubuntu $(lsb_release -sc) main" | sudo tee /etc/apt/sources.list.d/openresty.list $ sudo apt-get update $ sudo apt-get -y install openresty ``` 設置環境變量 ``` $ sudo vim ~/.bashrc /usr/local/openresty/nginx/sbin/ $ source ~/.bashrc ``` ## 開始第一個程序 創建項目目錄 ``` mkdir ~/openrestry cd ~/openrestry mkdir logs/ conf/ ``` 寫配置文件 ``` worker_processes 1; error_log logs/error.log; events { worker_connections 1024; } http { server { listen 8080; location / { default_type text/html; content_by_lua_block { ngx.say("<p>hello, world</p>") } } } } ``` 啟動 ``` nginx -p `pwd`/ -c conf/nginx.conf ``` 驗證是否成功 ``` curl http://localhost:8080/ ``` 返回結果` <p>hello, world </p>` ## OpenResty使用lua腳本寫http 請求 lua腳本可以寫在 conf/nginx.conf配置文件的 localtion 中.也可以寫在單獨的lua腳本文件中.需加上配置參數 `content_by_lua_file` ``` worker_processes 1; error_log logs/error.log; events { worker_connections 1024; } http { server { listen 8080; location / { default_type text/html; content_by_lua_file hello.lua; } } } ``` ### Get請求 hello.lua腳本 ``` //hello.lua local http = require('resty.http') local httpc = http.new() local res, err = httpc:request_uri('http://localhost/hello', { keepalive_timeout = 2000 -- 毫秒 }) if not res or res.status then ngx.log(ngx.ERR, "request error#", err) return end ngx.log(ngx.ERR, "request status#", res.status) ngx.say(res.body) ``` ### Post請求 hello.lua腳本 ``` //hello.lua local http = require "resty.http" local httpc = http.new() local res, err = httpc:request_uri("http://localhost/hello", { method = "POST", body = "a=1&b=2", headers = { ["Content-Type"] = "application/x-www-form-urlencoded", }, keepalive_timeout = 60, keepalive_pool = 10 }) if not res or res.status then ngx.log(ngx.ERR, "request error#", err) return end ngx.log(ngx.ERR, "request status#", res.status) ngx.say(res.body) ``` ### 重啟nginx ``` nginx -p `pwd`/ -s reload ``` ### 驗證 ``` curl http://localhost:8080 ``` ## 常用方法 創建`HTTP`請求對象,失敗返回`nil`和錯誤信息。 ``` httpc = http.new() ``` 發送請求,填入請求鏈接和參數。 ``` res, err = httpc:request_uri(uri, params) ``` 設置請求超時時間,單位:毫秒。 ``` httpc:set_timeout(time) ``` ## 遇到的問題 ### OpenResty返回中文亂碼問題 #### 描述 接口中使用`ngx.say(json.encode)`編碼`json`字符串,返回中文亂碼。 ``` {"msg":"èˉ·?±???????","code":0}` ``` #### 解決 設置`Content-Type`時加上`charset=utf8`。 ``` server { listen 8080; server_name localhost; default_type 'applicaiton/json;charset=utf8'; charset utf-8; ... } ``` 也可以在`header_filter`階段賦值`header`。 ``` ngx.header.content_type="application/json;charset=utf8" ```
                  <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>

                              哎呀哎呀视频在线观看