nginx请求处理过滤示例

来源:互联网 发布:java string 字符集 编辑:程序博客网 时间:2024/04/20 11:10

下面的代码段通过修改hello world模块的源文件ngx_http_hello_module.c,实现把对http://XXX.XXX.XXX.XXX/hello2/index.html的请求转发到/hello2/xndex.html文件上。这样的功能是为了把用户请求的连接地址定位到后端的文件系统,读取文件。因为文件系统存储的文件名可能不是index.html而是uuid或者是一个hash值。

#include <ngx_config.h>

#include <ngx_core.h>
#include <ngx_http.h>
typedef struct {
ngx_flag_t flag;
} ngx_http_hello_conf_t;


//static void ngx_http_hello_cleanup(void *data);
static void *ngx_http_hello_create_conf(ngx_conf_t *cf);
static char *ngx_http_hello_merge_conf(ngx_conf_t *cf, void *parent, void *child);
static ngx_int_t ngx_http_hello_init(ngx_conf_t *cf);


static char *
ngx_http_hello_set(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_conf_set_flag_slot(cf,cmd,conf);
return NGX_CONF_OK;
}


static ngx_command_t ngx_http_hello_commands[] =
{
{
        ngx_string("hello"),
        NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
        ngx_http_hello_set,
        NGX_HTTP_LOC_CONF_OFFSET,
        0,
        NULL
    },


ngx_null_command
};


static ngx_http_module_t ngx_http_hello_module_ctx =
{
NULL, /* preconfiguration */
ngx_http_hello_init, /* postconfiguration */
NULL, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
ngx_http_hello_create_conf, /* create location configration */
ngx_http_hello_merge_conf /* merge location configration */
};


ngx_module_t ngx_http_hello_module =
{
NGX_MODULE_V1,
&ngx_http_hello_module_ctx, /* module context */
ngx_http_hello_commands, /* module directives */
NGX_HTTP_MODULE, /* module type */
NULL, /* init master */
NULL, /* init module */
NULL, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
NULL, /* exit process */
NULL, /* exit master */
NGX_MODULE_V1_PADDING
};


typedef struct _hello_file_type {
    u_char * name;
    int mode;
    int create;
    int access;
}hello_file_type, *phello_file_type;


char* ngx_http_jcach_get_url(ngx_http_request_t *r)
{
char *url = NULL;
url = ngx_pcalloc(r->pool, r->uri.len + 1);
ngx_cpystrn((u_char*) url, r->uri.data, r->uri.len + 1);
return url;
}


static ngx_int_t
ngx_http_hello_handler(ngx_http_request_t *r)
{
char *url = ngx_http_jcach_get_url(r);
    ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
                      "current url is %s",
                      url);
    ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
                      "current url is %s",
                      url);


    //call jcs, get file path
    //jcs_get_file_phsical_path();


    //add to job list to inform jct




    if (strlen(url) == strlen("/hello2/index.html")) {
        if (0 == memcmp(url, "/hello2/index.html", strlen("/hello2/index.html")+1)) {
            r->uri.data[8] = 'X';
        }
    }
//ngx_http_send_header(r);
//ngx_http_output_filter(r, &out);
//ngx_http_finalize_request(r, rc);
return NGX_OK;


}


static void *
ngx_http_hello_create_conf(ngx_conf_t *cf)
{
    ngx_http_hello_conf_t *conf;


    conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_hello_conf_t));
    if (conf == NULL) {
        return NGX_CONF_ERROR;
    }
    /*
    * set by ngx_pcalloc():
    *
    * conf->shm_zone = NULL;
    * conf->conn = 0;
    */


    return conf;
}


static char *
ngx_http_hello_merge_conf(ngx_conf_t *cf, void *parent, void *child)
{
    ngx_http_hello_conf_t *prev = parent;
    ngx_http_hello_conf_t *conf = child;


    if (conf->flag == 0) {
        *conf = *prev;
    }


    return NGX_CONF_OK;
}


static ngx_int_t
ngx_http_hello_init(ngx_conf_t *cf)
{
    //ngx_http_core_loc_conf_t *clcf;


    //clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
    //clcf->handler = ngx_http_hello_handler;
    ngx_http_core_main_conf_t *cmcf;
    ngx_http_handler_pt        *h;


    cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);


    h = ngx_array_push(&cmcf->phases[NGX_HTTP_PREACCESS_PHASE].handlers);
    if (h == NULL) {
        return NGX_ERROR;
    }
    *h = ngx_http_hello_handler;


    return NGX_OK;
}


原创粉丝点击