openresty配置


openresty配置的一些問題總結!

環境

  • win10下的linux子系統ubuntu(wsl)

  • openresty版本:ngx_openresty-1.7.7.2.tar.gz

wsl安裝

請移步 {% post_link wsl筆記 %}

openresty安裝

請參考安裝OpenResty(Nginx+Lua)開發環境

或者參考 {% post_link openresty安裝筆記 %}

openssl版本問題

需要指定舊版本,推薦在編譯 Nginx 時指定 OpenSSL 源碼目錄,而不是使用系統自帶的版本,這樣更可控。

參考openssl版本問題

Lua模塊安裝

  1. 安裝 LuaRocks

    LuaRocks: Lua 的模塊安裝和部署工具

    apt-get install luarocks
    
  2. 安裝 luasocket

    lua 遠程調試引入的 require("mobdebug").start() 需要 socket

    :Lua 的 Remote Debug 遠程調試 現在還搞不定,此處占個坑。

    luarocks install luasocket
    

openresty使用

nginx常用命令

  1. 啟動:

    指定配置文件

    nginx -p `pwd`/ -c conf/nginx.conf
    
  2. 停止:

    nginx指定配置文件的,停止時也需指定參數

    nginx -p `pwd`/ -c conf/nginx.conf -s quit
    

nginx.conf 配置

  • 正則匹配路徑

    模式 含義
    location = /uri = 表示精確匹配,只有完全匹配上才能生效
    location ^~ /uri ^~ 開頭對URL路徑進行前綴匹配,並且在正則之前。
    location ~ pattern 開頭表示區分大小寫的正則匹配
    location ~* pattern 開頭表示不區分大小寫的正則匹配
    location /uri 不帶任何修飾符,也表示前綴匹配,但是在正則匹配之后
    location / 通用匹配,任何未匹配到其它location的請求都會匹配到,相當於switch中的default
    location ~ ^/api/([-_a-zA-Z0-9]+) {
        # 准入階段完成參數驗證
        access_by_lua_file  nginx_test_server/access_check.lua;
    
        #內容生成階段
        content_by_lua_file nginx_test_server/$1.lua;
    }
    
  • 開發調試時取消緩存

    # 這里設置為 off,是為了避免每次修改之后都要重新 reload 的麻煩。
    # 在生產環境上務必確保 lua_code_cache 設置成 on。
    lua_code_cache off;
    
  • http 模塊 報錯 bad argument #2 to 'set_keepalive' (number expected, got nil)

    參考:bad argument #2 to 'set_keepalive' (number expected, got nil)的解決辦法

    在關聯數組中多傳一個參數keepalive=false 即

    {
    method = “GET”,
    path = requestBody,
    keepalive=false
    }
    

lua 的一些坑

  • openresty/lua-resty-redis 的 批量查詢 返回值問題

    參考:nil、null與ngx.null

    官方說明

    A non-nil Redis "bulk reply" results in a Lua string as the return value. A nil bulk reply results in a ngx.null return value.

    A nil multi-bulk reply returns in a ngx.null value.

    如果使用批量查詢如mget,查不到數據會返回 ngx.null

OpenResty緩存

需要先在 nginx.conf 里面的修改 ,這個 cache 是 Nginx 所有 worker 之間共享的

lua_shared_dict my_cache 128m;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM