nginx的http模块开发--一个验证url参数的例子

简介:

本文开发一个nginx http模块基本的开发步骤和前篇http://www.cnblogs.com/yjf512/archive/2013/06/10/3130890.html 说的一样,按照开发的六个步骤写。

配置文件及功能

该模块的功能是验证请求url中的secret参数的值是否是约定的秘钥。

它的nginx配置文件是这样的:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
worker_processes  1;
 
error_log  logs/error.log debug;
master_process off;
daemon off;
 
events {
     worker_connections  1024;
}
 
 
http {
     default_type  application/octet-stream;
 
     sendfile        on;
 
     keepalive_timeout  65;
 
     server {
         listen       8001;
         server_name  localhost;
 
         access_log  /tmp/access.log;
         error_log   /tmp/error.log debug;
 
         location / {
             root   html;
             index  index.html index.htm;
         }
 
         error_page   500 502 503 504  /50x.html;
         location = /50x.html {
             root   html;
         }
 
         location = /mysecert {
             # 只有传递的secret参数值为secretpassword的时候才通过验证
             # 1 通过验证页面显示“secret right”
             # 2 不通过验证页面显示“secret wrong”
             # 比如
             # http: //abc.com:8001?secret=secretpassword通过
             # http: //abc.com:8001?secret=123不通过
             mysecret secretpassword;
         }
     }
 
}

 

location为/mysecret的时候,需要传递值为secretpassword的mysecret参数。页面会返回200并显示secret right,否则页面返回200并显示secret wrong。

配置文件这里有几个地方注意下:

为了调试方便,调整了几个地方:

1
2
3
4
5
6
worker_processes  1;
master_process off;
daemon off;
access_log  /tmp/access.log;
error_log   /tmp/error.log debug;

而且在configure的时候我也加上了--with-debug参数

1
./configure --add-module=/home/yejianfeng/nginx/nginx_module/mysecret2/ --prefix=/home/yejianfeng/nginx/nginx/ --with-debug

这样让调试更加方便

具体代码

完整的代码可以看:https://github.com/jianfengye/MyWorks/tree/master/nginx_module_mysecret

有几个地方要说明:

1 这个模块由于有从配置文件中读取的信息,所以它是有属于自己模块的配置文件结构的

1
2
3
4
5
typedef struct {
 
      ngx_str_t secret;
 
} ngx_http_mysecret_conf_t;

所以有自己的配置文件,那么在模块构造模块上下文的时候create_loc_conf的阶段就多一个功能是初始化配置文件结构

// 定义上下文, 只会在location中出现,所以都为null

static ngx_http_module_t ngx_http_mysecret_module_ctx = {

     NULL,

     NULL,

     NULL,

     NULL,

     NULL,

     NULL,

     ngx_http_mysecret_create_loc_conf,  //这里有使用自己的配置结构

     NULL

};

其中ngx_http_mysecret_create_loc_conf只要做的事情是初始化配置文件

2 如何将配置文件nginx.conf中的mysecret的一个参数放在配置文件结构中呢?

在ngx_http_mysecret这个命令回调函数中,有个ngx_conf_set_str_slot,这个是nginx预设的14个读取配置文件的函数之一,调用它就可以把nginx.conf中的secrect秘钥读取到配置文件结构中了。

3 在handler中怎么获取配置文件结构呢?

现在读取配置文件结束了,也构造了配置文件结构,到具体的处理http请求的阶段,如何获取这个结构呢?

1
2
3
ngx_http_mysecret_conf_t *mycf;
 
mycf = ngx_http_get_module_loc_conf(r, ngx_http_mysecret_module);

使用这个方法就能在handler中获取到自定义的配置文件结构了。

获取配置结构后,后面的问题就是如何获取请求参数呢

ngx_http_request_t中的args参数就是获取请求参数的

比如http://abc.com?a=s&b=2 那么args就是ngx_string("a=s&b=2")

后面就可以使用nginx自定义的ngx_strncasecmp进行字符比较等操作了。

好了,完整的一个验证请求参数的模块就写完了。

模块变种

有人会对nginx.conf文件有点不舒服,可能希望设置验证秘钥是分为两个步骤:

设置秘钥和验证秘钥

即配置文件大致变成现在的样子:

1
2
3
4
5
6
7
8
9
10
location = /mysecert {
             # 只有传递的secret参数值为secretpassword的时候才通过验证
             # 1 通过验证页面显示“secret right”
             # 2 不通过验证页面显示“secret wrong”
             # 比如
             # http: //abc.com:8001?secret=secretpassword通过
             # http: //abc.com:8001?secret=123不通过
             setmysecret secretpassword;
             checksecret;
  }

原来的mysecret被两个命令setmysecret和checksecret替换了

这两个命令的功能其实是不一样的,setmysecret只是读取配置文件,并不会对请求做任何操作,而checksecret是直接修改请求的。

其实上一个例子稍微改一改就可以达到这样的目的:

https://github.com/jianfengye/MyWorks/tree/master/nginx_module_mysecret2

定义模块命令的结构就变成:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
static  ngx_command_t ngx_http_mysecret_commands[] = {
      {
           ngx_string( "setmysecret" ),
           NGX_HTTP_LOC_CONF | NGX_CONF_TAKE1,
           ngx_conf_set_str_slot,
           NGX_HTTP_LOC_CONF_OFFSET,
           offsetof(ngx_http_mysecret_conf_t, secret),
           NULL,
      },
      {
           ngx_string( "checksecret" ),
           NGX_HTTP_LOC_CONF | NGX_CONF_NOARGS,
           ngx_http_mysecret,
           NGX_HTTP_LOC_CONF_OFFSET,
           0,
           NULL,
      },
 
      ngx_null_command
};

这里的setmysecret直接使用nginx预设的ngx_conf_set_str_slot方法,它就不需要有任何定义handler的操作了。事实上,nginx很多模块的像setmysecret这样的读取配置文件的命令(不做任何http请求的操作)都是直接使用nginx预设的14种方法的。具体的使用在《深入理解Nginx》第四章中有详细说明了。

自然在checksecret命令中就不需要再读取参数了(也没有参数了,所以要注意这个命令中的命令类型要设置上NGX_CONF_NOARGS)




本文转自轩脉刃博客园博客,原文链接:http://www.cnblogs.com/yjf512/archive/2013/06/13/3133569.html,如需转载请自行联系原作者

相关文章
|
1月前
|
Web App开发 编解码 运维
LNMP详解(十二)——Nginx URL重写实战
LNMP详解(十二)——Nginx URL重写实战
20 2
|
4月前
|
应用服务中间件 nginx
百度搜索:蓝易云【利用nginx内置ngx_http_mirror_module模块实现流量复制及流量放大】
以上就是使用Nginx内置 `ngx_http_mirror_module`模块实现流量复制和流量放大的简要示例。通过合理配置和利用该模块,可以实现更复杂的流量控制和调试需求。
63 1
|
4月前
|
应用服务中间件 nginx
百度搜索:蓝易云【HTTP请求是如何关联Nginx server{}块的?】
总结来说,Nginx中的 `server{}`块用于关联HTTP请求和虚拟主机,通过配置不同的 `server{}`块,可以实现多个域名或IP地址的请求分发和处理。这样,Nginx可以根据不同的请求来提供不同的服务和内容。
38 0
|
16天前
|
移动开发 前端开发 JavaScript
前端vue2、vue3去掉url路由“ # ”号——nginx配置(一)
前端vue2、vue3去掉url路由“ # ”号——nginx配置
49 0
|
3月前
|
机器学习/深度学习 前端开发 JavaScript
源映射错误:Error: request failed with status 404 源 URL:http://localhost:8080/bootstrap/js/axios-0.18.0.js
源映射错误:Error: request failed with status 404 源 URL:http://localhost:8080/bootstrap/js/axios-0.18.0.js
50 0
源映射错误:Error: request failed with status 404 源 URL:http://localhost:8080/bootstrap/js/axios-0.18.0.js
|
16天前
|
前端开发 JavaScript 应用服务中间件
前端vue2、vue3去掉url路由“ # ”号——nginx配置(二)
前端vue2、vue3去掉url路由“ # ”号——nginx配置
47 0
|
3月前
|
缓存 负载均衡 应用服务中间件
如何在 CentOS 7 上为 NGINX 安装开源 HTTP 加速器:Varnish
如何在 CentOS 7 上为 NGINX 安装开源 HTTP 加速器:Varnish
72 1
如何在 CentOS 7 上为 NGINX 安装开源 HTTP 加速器:Varnish
|
3天前
|
网络协议 应用服务中间件 nginx
nginx 302 301 设置 url 转跳 nginx 资源重定向 nginx tcp 和 http 转发
nginx 代理后端网站,和 网站资源目录重定向到其他连接地址
34 3
|
3天前
|
应用服务中间件 nginx
Nginx的referer参数的用法和原理
总结:referer参数可以用于Nginx配置,以限制或允许特定来源网站的访问,提高安全性或控制流量。它通过valid_referers指令来定义合法的Referer来源,并根据配置对请求进行处理。但需要注意,Referer字段内容可以被伪造,因此不应作为唯一的安全措施。
15 0
|
1月前
|
数据采集 缓存 监控
HTTP与URL基础解析及简单示例实践
HTTP与URL基础解析及简单示例实践