Chinaunix首页 | 论坛 | 博客
  • 博客访问: 155364
  • 博文数量: 22
  • 博客积分: 425
  • 博客等级: 下士
  • 技术积分: 350
  • 用 户 组: 普通用户
  • 注册时间: 2012-05-15 09:43
个人简介

专注服务器设计、开发; https://github.com/justscu;

文章分类

全部博文(22)

文章存档

2014年(10)

2013年(2)

2012年(10)

分类: 服务器与存储

2014-01-27 09:57:00

本文编译nginx-1.4.4源码和可选模块push-stream-module。
使用http技术实现管道长连接,可以用于消息的推送。用pub/sub模式来管理长链接,一端sub,一端pub。当pub发送消息时,所有的sub都可以收到该消息。
代码主页:

0 Configure


    ./configure --help, 列出所有的可配置选项;
    --prefix=~/commlib/u01/nginx-1.4.4/, 被安装在指定的目录下(~/commlib/u01/nginx-1.4.4/)
    --bindir=DIR,指定二进制文件的安装位置,这里的二进制文件定义为可以被用户直接执行的程序。
    --with-PACKAGE[=ARG],指定特定依赖包的源文件位置(--with-openssl=~/commlib/src/openssl-1.0.1c/)
    --add-module,指定特定模块的源文件位置(--add-module=~/commlib/src/nginx-push-stream-module-master/)
    --without,不使用某个模块(--without-http_rewrite_module


1 编译、安装、运行


1.1 编译
假设nginx-1.4.4放到"~/commlib/src/nginx-1.4.4"目录,nginx-push-stream-module放到"~/commlib/src/nginx-push-stream-module-master"目录。
(1) 进入到nginx目录, cd  ~/commlib/src/nginx-1.4.4/
(2) 配置:./configure --prefix=~/commlib/u01/nginx-1.4.4 --add-module=~/commlib/src/nginx-push-stream-module-master/  --with-http_ssl_module  --with-openssl=~/commlib/src/openssl-1.0.1c/ [注:openssl的源文件路径]

点击(此处)折叠或打开

  1. Configuration summary
  2. + using system PCRE library
  3. + using OpenSSL library: ~/commlib/u01/openssl-1.0.1c/
  4. + md5: using system crypto library
  5. + sha1: using system crypto library
  6. + using system zlib library

  7. nginx path prefix: "~/commlib/u01/nginx-1.4.4"
  8. nginx binary file: "~/commlib/u01/nginx-1.4.4/sbin/nginx"
  9. nginx configuration prefix: "~/commlib/u01/nginx-1.4.4/conf"
  10. nginx configuration file: "~/commlib/u01/nginx-1.4.4/conf/nginx.conf"
  11. nginx pid file: "~/commlib/u01/nginx-1.4.4/logs/nginx.pid"
  12. nginx error log file: "~/commlib/u01/nginx-1.4.4/logs/error.log"
  13. nginx http access log file: "~/commlib/u01/nginx-1.4.4/logs/access.log"
  14. nginx http client request body temporary files: "client_body_temp"
  15. nginx http proxy temporary files: "proxy_temp"
  16. nginx http fastcgi temporary files: "fastcgi_temp"
  17. nginx http uwsgi temporary files: "uwsgi_temp"
  18. nginx http scgi temporary files: "scgi_temp"
(3) make & make install
1.2 测试
cd  ~/commlib/u01/nginx-1.4.4/sbin;
./nginx -c ~/commlib/src/nginx-push-stream-module-master/misc/nginx.conf  -t  
其中: -c: 指定配置文件;  -t: 测试
若出现 nginx: the configuration file ~/commlib/src/nginx-push-stream-module-master/misc/nginx.conf syntax is ok
            nginx: configuration file ~/commlib/src/nginx-push-stream-module-master/misc/nginx.conf test is successful
表明成功。
1.3 运行
./nginx -c ~/commlib/src/nginx-push-stream-module-master/misc/nginx.conf
1.4 增加任意启动目录
        这样,只能在 ~/commlib/u01/nginx-1.4.4/sbin目录下,才可以用 ./nginx启动。若要在任意路径下都可以启动,可以修改~/.bashrc文件。
        alias nginx='~/commlib/u01/nginx-1.4.4/sbin/nginx',然后执行命令:source .bashrc

2 编译过程中问题的解决


        在configure和make过程中,可能会出现错误。如PCRE library is not found; sha1 library is not found需要安装PCRE和sha1库。
        我在安装的过程中,PCRE可以顺利的解决。但sha1,总是报错。
Configuration summary
  + using system PCRE library
  + using OpenSSL library: /home/ll/commlib/openssl-1.0.1c
  + using builtin md5 code
  + sha1 library is not found
  + using system zlib library
---------------------------------------
In file included from src/core/ngx_crypt.c:12:
src/core/ngx_sha1.h:19: fatal error: sha.h: No such file or directory
compilation terminated.
make[1]: *** [objs/src/core/ngx_crypt.o] Error 1
make[1]: Leaving directory `/home/ll/work/nginx-1.4.4'
make: *** [build] Error 2
        由于openssl中包含sha1的功能,也可以用openssl提供的功能,即libcrypto库来解决。首先看看系统中是否含有libcrypto库(执行whereis libcrypto命令),若没有的话,可以将openssl编译后生成的libcrypto.a拷贝到/user/lib目录下,并执行sudo ldconfig命令。之后再重新配置(./configure)。
Configuration summary
  + using system PCRE library
  + using OpenSSL library: /home/ll/commlib/openssl-1.0.1c
  + md5: using system crypto library
  + sha1: using system crypto library
  + using system zlib library

        至此,问题顺利解决。
       执行sudo ldconfig -p,可以查看系统中库的位置。


3 测试


    利用curl,对nginx的pub/sub功能进行简单测试。curl是文件传输工具,可以发送/接收http数据包。

    (1)启动nginx,命令为:./nginx -c ~/commlib/src/nginx-push-stream-module-master/misc/nginx.conf。这里用的是push-stream-module的配置。假设nginx服务器的地址和端口为 10.15.62.31:10800。

    (2)订阅sub。起命令行,输入:curl ,在命令行下会打印给nginx服务器发的http协议的内容(GET)。

    (3)发布pub。另起命令行,输入:curl -d helloworld 。该命令是向nginx的c1通道发布一条数据(POST),数据的内容为helloworld。

        此时,在订阅命令行下,可以看到curl收到的消息。
   (4)删除delete。另起命令行,输入:curl -X DELETE ''。该命令是让nginx删除c1通道(DELETE)。之后除非重新订阅,否则pub无效。

    (5)测试发现,客户端可以启动5W条链接至nginx,同时每条链接进行5个不同的订阅。nginx的性能完全没有问题。

        在测试中,发现open too many files错误是,可能是你系统的fd太少(用ulimit -a查看),调大即可,具体方法见TCP状态之CLOSE_WAIT

4 为Nginx增加动态库


    (1)生成动态库文件(.so文件)

点击(此处)折叠或打开

  1. // print.h 头文件
  2. #pragma once
  3. extern "C" int print_so(int);

  4. // print.cpp 文件,只是进行简单的打印输出
  5. #include "print.h"
  6. #include <stdlib.h>
  7. #include <stdio.h>

  8. int print_so(int num)
  9. {
  10.     printf("----------------you in put %d --- for print_so test\n", num);
  11.     return 0;
  12. }

  13. // CMakeLists.txt 文件
  14. cmake_minimum_required(VERSION 2.8)

  15. PROJECT(print_so)

  16. AUX_SOURCE_DIRECTORY(./ SRC_LIST)
  17. #SET(CMAKE_CXX_FLAGS "-fPIC -shared")

  18. SET(LIBRARY_OUTPUT_PATH ./)

  19. ADD_DEFINITIONS(-g -fPIC -shared -W -Wall -D_REENTRANT -D_FILE_OFFSET_BITS=64)

  20. INCLUDE_DIRECTORIES(./ ../)

  21. LINK_DIRECTORIES()

  22. ADD_LIBRARY(print_so SHARED ${SRC_LIST})
    cmake .;make 后生成libprint_so.so动态库。在库中,有函数int print_so(int num)。
    用nm libprint_so.so命令,可以查看.so文件导出的print_so函数地址信息和名字:00000000000005bc T print_so
     由于nginx是C工程,所以在导出函数时,需要导出的函数按照C函数的规则来命名(需要用extern "C" 声明)。
    (2)修改nginx的nginx.c文件,在末尾加上以下代码

点击(此处)折叠或打开

  1. #include <dlfcn.h>
  2. #include "/home/ll/work/so_create/print.h"

  3. void test_dll()
  4. {
  5.     printf("main print\n");

  6.     void* handler = dlopen("/home/ll/work/so_create/libprint_so.so", RTLD_LAZY);
  7.     if(NULL == handler)
  8.     {
  9.         fprintf(stderr, "%s\n", dlerror());
  10.         exit(EXIT_FAILURE);
  11.     }

  12.     int (*caller)(int);
  13.     caller = (int(*)(int)) dlsym(handler, "print_so");
  14.     if(caller == NULL)
  15.     {
  16.         fprintf(stderr, "%s\n", dlerror());
  17.         exit(EXIT_FAILURE);
  18.     }

  19.     caller(5);
  20.     dlclose(handler);
  21. }
    (3)修改makefile文件

第一种方法:在执行./cofigure后,会生成objs目录,及Makefile文件。修改objs/Makefile文件。

在$(LINK)后面,加上对应的so文件路径

objs/src/http/modules/ngx_http_upstream_keepalive_module.o \

objs/addon/src/ngx_http_push_stream_module.o \

objs/ngx_modules.o \

-L/home/ll/work/so_create -ldl -lprint_so -lpthread -lcrypt -lpcre -lcrypto -lcrypto -lz

第二种方法:在执行./configure命令之前,修改auto/options文件。

NGX_DEBUG=NO

#NGX_CC_OPT=

# 不优化,便于调试,加上调试信息

NGX_CC_OPT="-O0 -g" 

# 新增加的so文件及其路径

NGX_LD_OPT="-L/home/ll/work/so_create -ldl -lprint_so"

CPU=NO

    (4)编译,运行
                   make & make install

阅读(6027) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~