跳到主內容

【Openresty】Nginx Lua的 執行階段

 

#config 區塊 基本配置
...                   # 全域性區塊
 
event{                # events 區塊
        ...
}
http{                 # http 區塊
  
    init_by_lua_block{
    }
    
    init_by_lua_file /usr/local/nginx/conf/lua/init.lua
    
    #常見的功能是執行定時任務 or health_check
    #此階段一般用來進行權限檢查和黑白名單配置
    init_worker_by_lua_block{
  
  	}
  
    #配置環境:**http,server,location,location if
  	access_by_lua_block{}
  
  	#配置環境**http,server,location,location if#
    #常用於header進行添加、刪除等操作
    header_filter_by_lua_block{}
    
    server{           # server 區塊
        location /aaa {     # location 區塊
        	set $b '';
      		#  設定變量
            set_by_lua_block $a {
                local t = 'test'
                ngx.var.b = 'test_b'
                return t
            }
            return 200 $a,$b; # test,test_b
            }
    
        location /bbb {
          set $b  '1';
      	  #rewrite_by_lua_block始終都在rewrite階段的後面執行
          rewrite_by_lua_block { ngx.var.b = '2'}
          set $b  '3';
            echo $b; #2
          }
        }
  		
  		location /ccc{
    		#**配置環境:**location,location if
    		#content_by_lua_block指令不可以和其他內容處理階段的模塊如echo、return、proxy_pass等放在一起
    		content_by_lua_block{}
  		}
  
        location /ddd{
            #content_by_lua_file可以直接获取URL中参数file_name的值
            content_by_lua_file conf/lua/$arg_file_name;
        }
  
        location /eee{
    		#将响应体全部转换为大写
    		#**配置環境:**http,server,location,location if
    		body_filter_by_lua_block { ngx.arg[1] = string.upper(ngx. arg[1]) }
        }
    }
}

https://blog.51cto.com/xikder/2331649