到nginx.org上,下載對應的nginx
測試nginx是否可以安裝
./configure
如果出現
./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.
執行:
sudo aptitude install libpcre3-dev
如果還缺少其他包,繼續安裝。直到./configure沒有錯誤
sudo make
sudo make install
啟動nginx,sudo /usr/local/nginx/sbin/nginx
另外啟動一個命名窗口:
安裝curl
sudo apt-get install curl
執行curl -i http://localhost
或者用瀏覽器訪問http://localhost/
就可以看到welcome to nginx的歡迎字幕 表示安裝成功
加載Empty Gif插件
在http://www.evanmiller.org/網站中
有篇文章http://www.evanmiller.org/nginx-modules-guide.html介紹了nginx的開發,比較全面,但較老,如果要詳細了解nginx的模塊開發,還得熟悉nginx的源代碼。
Empty Gif 較為簡單,主要的功能就是瀏覽器訪問某個地址,返回一個gif圖片。
如何編譯:
Empty Gif使用到了圖片庫,先安裝
sudo apt-get install libmagickwand-dev
下載Empty Gif代碼
安裝nginx模塊的一般步驟如下:
1、編寫模塊config文件,這個文件需要放在和模塊源代碼文件放在同一目錄下。文件內容如下:
ngx_addon_name=模塊完整名稱
HTTP_MODULES="$HTTP_MODULES 模塊完整名稱"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/源代碼文件名"
編寫源代碼文件,放在同一個目錄下
2、進入Nginx源代碼,使用命令編譯安裝
./configure --prefix=安裝目錄 --add-module=模塊源代碼文件目錄 --with-debug
make
sudo make install
3、修改nginx的配置文件
location /circles {
circle_gif;
}
4、啟動nginx
sudo /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
在瀏覽器中輸入
http://localhost/circles/ffffff/000000/20.gif
就可以看到
其中url的格式如下:
/circles/ffffff/000000/20.gif
/circles/<background color>/<foreground color>/<size>.gif
模塊是如何注冊
在nginx代碼的auto目錄中,有一個名為sources的文件,根據編譯選項(configure的參數)的不同,m4宏變量HTTP_MODULES的值會發生變化:
如果指定了使用empty gif模塊(默認就是使用了),則最終m4宏變量HTTP_MODULES的值可能如下:
HTTP_MODULES="ngx_http_module /
ngx_http_core_module /
ngx_http_log_module /
ngx_http_upstream_module /
ngx_http_empty_gif_module "
注意:這里的ngx_http_empty_gif_module字符串對應了ngx_http_empty_gif_module.c文件中的Module主結構變量名。
參考文章:
http://www.evanmiller.org/
http://www.evanmiller.org/nginx-modules-guide.html
http://www.162cm.com/p/ngx_ext.html
http://www.codinglabs.org/html/intro-of-nginx-module-development.html
http://blog.csdn.net/conezxy/article/details/1869130
http://ri0day.blogbus.com/logs/61820056.html
在http://wiki.nginx.org/3rdPartyModules 中有很多關于第三方模塊的開發,國人agentzh在這方面做了大量的工作。
posted on 2012-01-05 15:12
漂漂 閱讀(2488)
評論(0) 編輯 收藏 引用 所屬分類:
c++經典文章轉載