OpenResty 開發環境構建

基於kong環境構建

1、kong安裝完成後,停止kong服務;

kong  stop

2、關閉openresty模塊下nginx緩存

此操作爲了方便後續,更改代碼後,無需重啓nginx服務;

將  nginx.conf 對應 server 下 lua_code_cache 設置爲: off

[root@CentOS02 nginx]# cd /usr/local/openresty/nginx/
[root@CentOS02 nginx]# ls
client_body_temp  conf  fastcgi_temp  html  logs  lua_tmp  proxy_temp  sbin  scgi_temp  uwsgi_temp
[root@CentOS02 nginx]# vi conf/nginx.conf
.....
    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       80;
        lua_code_cache   off;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;

......

nginx.conf添加配置,調用lua文件:

rewrite_by_lua_file  /usr/local/openresty/nginx/lua_tmp/test.lua;

.....
    server {
        listen       80;
        lua_code_cache   off;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            index  index.html index.htm;
        }

        location /lua {
         # MIME type determined by default_type:
         set $my_var 123456;
         default_type 'text/plain';
         rewrite_by_lua_file  /usr/local/openresty/nginx/lua_tmp/test.lua;
       }

.....

test.lua 文件開發參考:

https://www.nginx.com/resources/wiki/modules/lua/

[root@CentOS02 lua_tmp]# cat test.lua 
ngx.header.content_type = 'text/plain';
ngx.header["Hi-World"] = {"hello world !","hello baby!"};


ngx.print(ngx.md5("ddd"));

啓動nginx

[root@CentOS02 nginx]# ./sbin/nginx 
nginx: [alert] lua_code_cache is off; this will hurt performance in /usr/local/openresty/nginx/conf/nginx.conf:37

 

調用演示

header 信息:

body信息:

 

引入自定義C++ 代碼

 

安裝 g++

yum install gcc-c++

 

聲明C代碼庫地址

vi ~/.bash_profile


export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/openresty/luajit/lib/lua/5.1/

 

編寫C代碼:

[root@CentOS02 5.1]# cat fast_sort.c 
/************************************************************
 *
 * gcc -g -o libluacallc.so -fPIC -shared fast_sort.c  
 *
 * *********************************************************/
int fast(int *a,int m,int n){  
	int key;
	int i,j;
	if(m<n){
		i=m;
		j=n;
		key=a[i];
		while(i<j){							
			while(i<j && a[j]>key){			
				j--;
			}
			if(i<j){
				a[i++]=a[j];				
				//i++;
			}
			while(i<j && a[i]<key){			
				i++;
			}
			if(i<j){
				a[j--]=a[i];				
				//j--;
			}
		}
		a[i]=key;
		fast(a,m,i-1);						
		fast(a,i+1,n);						
	}
	
}

編譯生成 libluacallc.so

 gcc -g -o libluacallc.so -fPIC -shared fast_sort.c 

將 libluacallc.so 已入庫地址: /usr/local/openresty/luajit/lib/lua/5.1/

編寫lua腳本

local ffi=require"ffi"  

local myffi=ffi.load("luacallc")  

--lua 數組映射到C層  
ffi.cdef[[  
int fast(int *t,int n,int m);  
]]    
local t={55,29,11,33,222,77,20,55,90,0}  
local len=#t  
local t_c=ffi.new("int[?]",len,t)  
local re=myffi.fast(t_c,0,len-1)  
--因爲得到的t_c爲cdata數據,需要轉換回lua數據  
local sorted_t={}  
for i=1,len do  
    sorted_t[i]=t_c[i-1]  
end  

ngx.header.content_type = 'text/plain';
ngx.header["Hi-World"] = {"hello world !","hello baby!"};


ngx.print("sorted t: ",table.concat(sorted_t,","))

 

重新調用:

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章