3. 如何在理解Nginx源碼的基礎(chǔ)上,通過編寫Nginx插件來提高自己的編程能力?
研究過一部分Nginx的源代碼,全部用C語言寫的,代碼質(zhì)量非常高。編寫Nginx插件,當(dāng)然需要掌握C語言,C++語言也是可以開發(fā)Nginx的,據(jù)我所知lua也可以編寫Nginx插件。
理解Nginx源碼最好的方法就是先用,在使用過程中加深理解。其次選一本好的參考書,輔助學(xué)習(xí),好的書籍能夠夠指導(dǎo)我們學(xué)習(xí),達(dá)到事半功倍的效果。
(1) 首先下載一份Nginx的源碼,搭建編譯環(huán)境,編譯安裝。這種方式難度會大一些,需要在源代碼目錄執(zhí)行./configure,然后make & make install,其次,要處理很多編譯依賴庫安裝的問題,例如pcre。例如下圖:
checking for getaddrinfo() ... found
checking for PCRE library ... not found
checking for PCRE library in /usr/local/ ... not found
checking for PCRE library in /usr/include/pcre/ ... not found
checking for PCRE library in /usr/pkg/ ... not found
checking for PCRE library in /opt/local/ ... not found
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.