Lua在Nginx中的应用
如果无法正常显示,请先停止浏览器的去广告插件。
1.
Application of Lua in Nginx
Lua 在 Nginx 中的应用
☺agentzh@gmail.com☺
章亦春 (agentzh)
2015.04
2.
3.
4.
5.
6.
7.
8.
☺ No "callback hell".
☺ 100% nonblocking IO
没有“回调地狱”。
100% 非阻塞I/O.
9.
10.
11.
12.
13.
☺ openresty.org
14.
☺ qa.openresty.org
OpenResty 测试集群
15.
16.
/ipthr t10 lnxx66'nxeh g_u
$.dsace r 7 a'iu 8_4 g_conxla
eursa es ahns
Rqie tlat5mcie.
ukt1 lnxla(6 i)
bce :tg_u 24mn
ukt2 wg_u wnxlatvnxeh hnxeh 17mn
bce :tvnxlatg_u rg_cotg_co(6 i)
ukt3 rg_u hnxlatnxlatg_cotg_co(6 i)
bce :tvnxlatg_u g_u vnxeh rnxeh 12mn
ukt4 vnxlatg_cotg_u hg_cotnxeh 15mn
bce :tg_u lnxeh onxlatvnxeh g_co(4 i)
ukt5 hg_u rnxlatvnxeh wnxeh onxeh 9 i)
bce :tvnxlatg_u wg_cotg_cotg_co(0mn
.
..
17.
18.
☺ Lua SSL
☺ Lua CDN
☺ Lua WAF (Lua Web 防火墙)
19.
☺ Light threads
轻量级线程
20.
21.
oa hedA r
lcltra_,er=
g.hedsanfn1
nxtra.pw(uc)
tra_ ep unn snhoosy
hedAkesrnigaycrnul
i h akrudo h urn
ntebcgon ftecret
"ih hed.
lgttra"
22.
oa k e1 e2=
lclo,rs,rs
g.hedwi(hedA hedB
nxtra.attra_,tra_)
23.
oa k r g.hedkl(hedA
lclo,er=nxtra.iltra_)
24.
☺ luarestywebsocket
WebSocket 服务器和客户端
25.
oa evr=rqie"et.esce.evr
lclsre eur rsywboktsre"
oa b r evrnw
lclw,er=sre:e{
ieu 00 nmlieod
tmot=50, i ilscns
a_ala_e 53,
mxpyodln=655
}
oa aa y,er=w:evfae)
lcldt,tp r brc_rm(
oa ye,er=w:edtx(Hlowrd)
lclbts r bsn_et"el ol"
26.
☺ fullduplex cosockets
全双工的基于协程的套接字
27.
☺ SSL/TLS cosocket
28.
oa ok=nxsce.c(
lclsc g.okttp)
oa k r okcnet"w.lufaecm,
lclo,er=sc:onc(wwcodlr.o"
43
4)
k r oksladhk(
o,er=sc:shnsae
as, dsbeSLssin
fle ial S eso
wwcodlr.o" N ae
"w.lufaecm, SInm
re vrf vrtig
tu eiyeeyhn
)
29.
☺ ssl_certificate_by_lua
纯 Lua 动态服务 SSL 证书和私钥
30.
31.
s_etfct_yla'
slcriiaeb_u
oa s eur nxsl
lclsl=rqie"g.s"
oa etcan rvky=m_oddt(
lclcr_hi,pi_e yla_aa)
setslstdrcr(etcan)
asr(s.e_e_etcr_hi)
setslstdrpi_e(rvky)
asr(s.e_e_rvkypi_e)
;
'
32.
33.
☺ ssl_balancer_by_lua
纯 Lua 编写 Nginx 全动态负载均衡器
34.
ptem{
usra
evr0000
sre ...;
aacrb_u
blne_yla'
oa aacr=rqie"g.aacr
lclblne eur nxblne"
oa ot ot=m_odbcedad(
lclhs,pr yla_akn_dr)
setblne.e_u_erhs,pr)
asr(aacrstcrpe(ot ot)
;
'
}
35.
☺ nginxsystemtaptoolkit
基于 Systemtap 的 Nginx 工具箱
36.
37.
☺ stapxx (stap++)
Systemtap 加上一点宏魔法……
38.
39.
☺ How CPU time is spent inside LuaJIT
CPU 时间在 LuaJIT 内部是如何分布的
40.
jvttssx 51agtm=0
$lmsae.x x52 r ie6
tr rcn 51(urlclnixwfsi/gna)
Sattaig52 /s/oa/gna/bnnixwf
laewi o 0scns.
Pes atfr6 eod..
bevd10 uunn ape n goe 9urltdsmls
Osre 23Larnigsmlsadinrd4 neae ape.
opld 4 79smls
Cmie:6%(7 ape)
oe(yitrrtdLa:1%(7 ape)
CCd b nepee u) 4 12smls
nepee:1%(5 ape)
Itrrtd 3 18smls
abg olco cmie) %(8smls
GraeCletr(opld:3 4 ape)
abg olco ntcmie) %(2smls
GraeCletr(o opld:3 4 ape)
rc xtn:0 4smls
Taeeiig %( ape)
41. ☺ When an nginx worker's CPU is too high...
当 nginx worker 进程的 CPU 太高时……
42.
43.
suigoenixwre rcs a h i 94.
#asmn n gn okrpoeshstepd167
gp.x x167
$nxrssx 94
ANN:Taigpoes167
WRIG rcn rcs 94.
i tlCt n.
HtCr oed
17994]30rqsc
[36353 0 e/e
17994]25rqsc
[36354 3 e/e
17994]25rqsc
[36355 3 e/e
17994]16rqsc
[36356 6 e/e
17994]28rqsc
[36357 3 e/e
17994]24rqsc
[36358 3 e/e
C
^
44.
/apeb p167 0 .t
$.smlt 94 t2 u>ab
ANN:Taig167(otnixsi/gn)i srsaeol..
WRIG rcn 94 /p/gn/bnnix nuepc ny.
ANN:Tm' p utignw.(tmytk hl)
WRIG iesu.Qitn o..i a aeawie
45.
sn rna rg' lm rp ol:
#uigBednGegsfaegahtos
tcclastpp .t>act
$sakolpesa.lab .b
lmgahp .b .v
$faerp.lact>asg
46.
47.
suigtenixwre rcs i s167
#asmn h gn okrpoespdi 94:
jlasak.x r ie2 kpbdas 94 .t
$lutcssxagtm=0siavr x167>ab
tr rcn 94 /p/gn/bnnix
Sattaig167(otnixsi/gn)
laewi o 0scns
Pes atfr2 eod
48.
sn rna rg' lm rp ol:
#uigBednGegsfaegahtos
tcclastpp .t>act
$sakolpesa.lab .b
lmgahp .b .v
$faerp.lact>asg
49.
50.
☺ When an nginx worker's CPU is low
and the throughput is low...
当 nginx worker 进程的 CPU 很低,
同时吞吐量上不去的时候……
51.
52.
☺ offCPU flame graphs
from the samplebtoffcpu tool
53.
54.
55.
56.
☺ nginxgdbutils
基于GDB 的Nginx 工具箱
57.
58.
gb b
(d)lt
:g_tplasce_c_eev
Cnxht_u_okttprcie
..lbrsymslla11
@./i/et/yq.u:9
..lbrsymslla50
@./i/et/yq.u:3
otn_yla1
cnetb_u:0
59.
gb b ul
(d)ltfl
:g_tplasce_c_eev
Cnxht_u_okttprcie
..lbrsymslla11
@./i/et/yq.u:9
oa sl"
lcl"ef:
al 04f8a)
tbe(x0118
oa sc"
lcl"ok:
al 04f8b)
tbe(x0110
..lbrsymslla50
@./i/et/yq.u:3
oa sl"
lcl"ef:
al 04f84)
tbe(x0118
oa ot"
lcl"ps:
al 04f85)
tbe(x0110
.
..
60.
gb g
(d)lc
h urn eoysz alctdb C:886 ye
Tecretmmr ie(loae yG) 990bts
61.
gb gsa
(d)lctt
57 t ojcs a=96 v 1 i=8 u=716
112sr bet:mx25,ag=5,mn1,sm792
97uvl bet:mx2,ag=2,mn2,sm268
8 pa ojcs a=4 v 4 i=4 u=38
14tra bet:mx14,ag=12,mn58 u=674
0 hed ojcs a=68 v 62 i=2,sm188
41poo bet:mx267,ag=23,mn7,sm939
3 rt ojcs a=224 v 24 i=8 u=616
92fn bet:mx14 v 0 i=0 u=80
5 uc ojcs a=4,ag=3,mn2,sm290
46tae bet:mx240 v 87 i=6,sm880
4 rc ojcs a=30,ag=15,mn10 u=264
95caa bet:mx41,ag=1,mn1,sm556
26 dt ojcs a=12 v 7 i=2 u=17
86 a ojcs a=40,ag=27 i=2 u=935
191tb bet:mx268 v 0,mn3,sm3426
9uaa bet:mx169,ag=333 i=2 u=582
dt ojcs a=705 v 91,mn3,sm332
62.
☺ Any questions? ☺
欢迎提问
63. A