<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之旅 廣告
                如何使用Lua編寫Hello World 為了方便開發,我們在這里指定一個訪問域名:http://openresty.tinywan.com 在`/usr/local/openresty/nginx/conf`目錄下創建一個`openresty.tinywan.com.conf` `openresty.tinywan.com.conf` 配置文件內容 ``` server { listen 80; server_name openresty.tinywan.com; } ``` ## 編寫location 在`openresty.tinywan.com.conf` 中`server`部分添加如下配置 ``` location /lua { default_type 'text/html'; content_by_lua 'ngx.say("Hello World")'; } ``` 先檢查Nginx配置文件語法是否正確 ``` /usr/local/openresty/nginx/sbin/nginx -t ``` 輸出以下內容表示配置 ``` nginx: the configuration file /usr/local/openresty/nginx/conf/nginx.conf syntax is ok nginx: configuration file /usr/local/openresty/nginx/conf/nginx.conf test is successful ``` 重新加載配置 ``` sudo systemctl restart openresty.service ``` 或者 ``` /usr/local/openresty/nginx/sbin/nginx -s reload ``` 瀏覽器訪問`http://openresty.tinywan.com/lua`應該返回`Hello World`。 ![](https://img.kancloud.cn/9a/ec/9aeccb68237f8ff70b44f7e5d4cf240e_989x642.png) ## 使用lua代碼文件 把lua代碼放在nginx配置中會隨著lua的代碼的增加導致配置文件太長不好維護,因此應該把lua代碼移到外部文件中存儲。 在`/usr/local/openresty/nginx/conf`目錄下創建一個`lua`目錄。新建一個`hello.lua`文件,添加如下內容 ``` ngx.say("Hello 開源技術小棧") ``` 修改`openresty.tinywan.com.conf` 配置 ``` server { listen 80; server_name openresty.tinywan.com; location /lua { default_type 'text/html'; content_by_lua_file conf/lua/hello.lua; #相對于nginx安裝目錄 } } ``` 此處`conf/lua/hello.lua`也可以使用絕對路徑`/usr/local/openresty/nginx/conf/lua/hello.lua`。 重啟Openresty,重新訪問 ``` curl -i http://openresty.tinywan.com/lua ``` ![](https://img.kancloud.cn/b5/33/b533232296b442241879faa44dc92ef0_989x276.png) ## lua_code_cache 默認情況下`lua_code_cache` 是開啟的,默認會緩存`lua`代碼,即每次`lua`代碼變更必須`reload nginx`才生效,如果在開發階段可以通過`lua_code_cache off;`關閉緩存,這樣調試時每次修改lua代碼不需要`reload nginx`,但是正式環境一定記得開啟緩存。 ``` server { listen 80; server_name openresty.tinywan.com; location /lua { default_type 'text/html'; lua_code_cache off; content_by_lua_file conf/lua/hello.lua; } } ``` 開啟后`reload nginx`會看到如下報警 ``` nginx: [alert] lua_code_cache is off; this will hurt performance in /etc/nginx/conf.d/openresty.tinywan.com.conf:7 ``` ## 錯誤日志 如果運行過程中出現錯誤,請不要忘記查看錯誤日志。 ``` tail -f /usr/local/openresty/nginx/logs/error.log ``` 錯誤內容 ``` 2024/07/13 13:13:48 [emerg] 1#1: invalid return code "Hello World!" in /etc/nginx/conf.d/tinywan_lua.conf:7 2024/07/13 13:14:01 [emerg] 1#1: invalid return code "Hello World!" in /etc/nginx/conf.d/tinywan_lua.conf:7 2024/07/13 13:14:28 [emerg] 1#1: invalid return code "Hello World!" in /etc/nginx/conf.d/tinywan_lua.conf:7 ```
                  <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>

                              哎呀哎呀视频在线观看