nginx+lua+redis构建高并发应用,转

nginx+lua+redis构建高并发应用

ngx_lua将lua嵌入到nginx,让nginx执行lua脚本,高并发,非阻塞的处理各种请求。

url请求nginx服务器,然后lua查询redis,返回json数据。

备注:centos或者redhat系统请跳转到nginx + ngx_lua安装测试

一.安装lua

1

2

3

# apt-get install lua5.1

# apt-get install liblua5.1-dev

# apt-get install liblua5.1-socket2

二.安装nginx

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

# apt-get install git-core

# git clone https://github.com/simpl/ngx_devel_kit.git

# git clone https://github.com/chaoslawful/lua-nginx-module.git

# git clone https://github.com/agentzh/redis2-nginx-module.git

# git clone https://github.com/agentzh/set-misc-nginx-module.git

# git clone https://github.com/agentzh/echo-nginx-module.git

# git clone https://github.com/catap/ngx_http_upstream_keepalive.git

# apt-get install libpcre3 libpcre3-dev libltdl-dev libssl-dev libjpeg62 libjpeg62-dev libpng12-0 libpng12-dev libxml2-dev libcurl4-openssl-dev libmcrypt-dev autoconf libxslt1-dev libgd2-noxpm-dev libgeoip-dev libperl-dev -y

# wget http://nginx.org/download/nginx-1.0.8.tar.gz

# tar zxvf nginx-1.0.8.tar.gz

# cd nginx-1.0.8

# ./configure --prefix=/usr/local/nginx --with-debug --with-http_addition_module \

\

\

\

\

\

\

\

\

\

\

\

module

# make

# make install

三.安装lua-redis-parser

1

2

3

4

# git clone https://github.com/agentzh/lua-redis-parser.git

# export LUA_INCLUDE_DIR=/usr/include/lua5.1

# make CC=gcc

# make install CC=gcc

四.安装json

1

2

3

# wget http://files.luaforge.net/releases/json/json/0.9.50/json4lua-0.9.50.zip

# unzip json4lua-0.9.50.zip

# cp json4lua-0.9.50/json/json.lua /usr/share/lua/5.1/

五.安装redis-lua

1

2

# git clone https://github.com/nrk/redis-lua.git

# cp redis-lua/src/redis.lua /usr/share/lua/5.1/

六.配置

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

;

;

;

;

;

;

{

;

;

}

{

;

;

;

;

;

;

;

;

;

;

{

;

//定义连接池大小,当连接数达到此数后,后续的连接为短连接

}

{

;

;

{

#internal;

;

;

;

}

{

;

}

}

}

# vim fuck.lua

1

2

3

4

5

6

7

8

9

10

11

12

)

)

{

}

)

then

)

)

)

)

)

end

七.测试

# redis-cli -h 192.168.1.39

1

2

3

com

OK

# curl 'http://192.168.1.211/json?key=ttlsa'

1

]

www.ttlsa.com

http://www.ttlsa.com/nginx/nginx-lua-redis/