首页 > 代码库 > nginx subrequest程序示例

nginx subrequest程序示例

仅仅简单的subrequest应用示例。 

nginx.conf文件:

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

#pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
        include       mime.types;
        default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;
    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
        keepalive_timeout  65;

    #gzip  on;

        server {
                listen       80;
                server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

                location / {
                        root   html;
                        index  index.html index.htm;
                }

#               location /test2 {  
#                       test_str "hello my dear HUST!";  
#                       test_flag on;  
#                       test_num 10;  
#                       test_size 1000;  
#                       mytest;  
#               }
                location /list {
                        proxy_pass http://hq.sinajs.cn;
                        proxy_set_header Accept-Encoding  "";
                }

                location /query {
                        mytest;
                }
#               location /test2 {
#                        test_str  "hello my dear ye.";
#                        test_flag  on;
#                        test_num   10;
#                        test_size  1000;
#                        mytest;
#                }

        #error_page  404              /404.html;

        # redirect server error pages to the static page /50x.html
        #
               error_page   500 502 503 504  /50x.html;
                location = /50x.html {
                        root   html;
                }


        }

        server {
                listen    80 default_server;
                server_name  www8.test.com ;
                root  /home/src/index;
                index  index.html;
                location /list {
                        proxy_pass http://hq.sinajs.cn;
                        proxy_set_header Accept-Encoding  "";
                }

                location /query {
                        mytest;
                }

#               location /test2 {
#                       test_str  "hello my dear ye.";
                   #                       test_flag  on;
#                       test_num   10;
#                       test_size  1000;
#                       mytest;
#               }

                error_page   500 502 503 504  /50x.html;
                location = /50x.html {
                       root   html;
                }

        }

}
                                       


config文件  注意与你写的模块放在同一个文件夹下面。

ngx_addon_name=ngx_http_mytest_module
HTTP_MODULES="$HTTP_MODULES ngx_http_mytest_module"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_mytest_subrequest_module.c"

ngx_http_mytest_subrequest_module.c文件

#include<ngx_http.h>
#include<ngx_config.h>
#include<ngx_core.h>

static void mytest_post_handler(ngx_http_request_t *r);
static ngx_int_t mytest_subrequest_post_handler(ngx_http_request_t *r,void *    data,ngx_int_t rc);
static ngx_int_t ngx_http_mytest_handler(ngx_http_request_t *r);
//save gupiao data
typedef struct {
        ngx_str_t stock[6];
}ngx_http_mytest_ctx_t;

static ngx_http_module_t ngx_http_mytest_module_ctx = {
        NULL,NULL,
        NULL,NULL,
        NULL,NULL,
        NULL,NULL
};
static char * ngx_http_mytest(ngx_conf_t *cf,ngx_command_t *cmd,void *conf){
        ngx_http_core_loc_conf_t *clcf;
        clcf = ngx_http_conf_get_module_loc_conf(cf,ngx_http_core_module);
        clcf->handler = ngx_http_mytest_handler;
        return NGX_CONF_OK;
}

static ngx_command_t ngx_http_mytest_commands[] = {
        {
                ngx_string("mytest"),
                NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_HTTP_LMT_CONF|NGX_CONF_NOARGS,
                ngx_http_mytest,
                NGX_HTTP_LOC_CONF_OFFSET,
                0,
                NULL
        },
        ngx_null_command
};

ngx_module_t ngx_http_mytest_module = {
        NGX_MODULE_V1,
        &ngx_http_mytest_module_ctx,
        ngx_http_mytest_commands,
        NGX_HTTP_MODULE,
        NULL,NULL,
        NULL,NULL,
        NULL,NULL,
        NULL,
        NGX_MODULE_V1_PADDING
};

//start subrequest 
static ngx_int_t ngx_http_mytest_handler(ngx_http_request_t *r){
        //creat HTTP ctx
        ngx_http_mytest_ctx_t *myctx = ngx_http_get_module_ctx(r,ngx_http_mytest_module);
        if(myctx == NULL){
                myctx = ngx_palloc(r->pool,sizeof(ngx_http_mytest_ctx_t));
                if(myctx == NULL) return NGX_ERROR;
                ngx_http_set_ctx(r,myctx,ngx_http_mytest_module);
        }
        ngx_http_post_subrequest_t *psr = ngx_palloc(r->pool,sizeof(ngx_http_post_subrequest_t));
        if(psr == NULL) return NGX_HTTP_INTERNAL_SERVER_ERROR;
        psr->handler = mytest_subrequest_post_handler;
        psr->data = http://www.mamicode.com/myctx;>

没有什么好说的,因为需要的人,自然会懂。