基于Nginx实现一个自己的HTTP模块
/usr/local/nginx/conf/nginx.conf文件例如以下:
#worker工作进程的用户及用户组
user weijl;
#Nginx worker进程个数
worker_processes 1; #error日志的设置,默认logs/error.log error
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info; #pid文件的路径
#pid logs/nginx.pid; events {
worker_connections 1024;
} http {
#嵌入配置文件mime.types
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; upstream test.proxy.com {
#ip_hash;
server 192.168.0.7;
server 192.168.0.8;
} server {
listen 127.0.0.1:80;
server_name test.proxy.com; #charset koi8-r; #access_log logs/host.access.log main; location / {
root html;
index index.html index.htm;
} #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;
} #静态图片资源
location /image/ {
root /home/weijl/workspace/;
autoindex on;
} #反向代理
location /proxy_loc/ {
root html;
proxy_set_header Host $host;
proxy_pass http://test.proxy.com;
#禁用缓存
proxy_buffering off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 100m;
} #实现自己的HTTP模块
location /test {
mytest;
root html;
} # proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#} # deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
} # another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias; # location / {
# root html;
# index index.html index.htm;
# }
#} # HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost; # ssl_certificate cert.pem;
# ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on; # location / {
# root html;
# index index.html index.htm;
# }
#} }
匹配uri,在/usr/local/nginx/html/文件夹下创建文件夹test:
weijl@weijl-ubuntu:/usr/local/nginx/html$ sudo mkdir test
weijl@weijl-ubuntu:/usr/local/nginx/html$ sudo chmod -R 777 test/
实现自己的HTTP模块C代码/home/weijl/workspace/nginx-1.10.3/src/http/ngx_http_mytest_module.c例如以下:
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h> //请求包体接收完后回调的函数
void ngx_http_mytest_body_handler(ngx_http_request_t *r)
{ } //HTTP的HTTP_CONTENT_PHASE阶段mytest模块介入处理http请求内容
static ngx_int_t ngx_http_mytest_handler(ngx_http_request_t *r)
{
//必须时GET或者HEAD方法,否则返回405 Not Allowed
if(!(r->method &(NGX_HTTP_GET | NGX_HTTP_HEAD)))
{
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "weijl NGX_HTTP_NOT_ALLOWED");
return NGX_HTTP_NOT_ALLOWED;
} //丢弃请求中的包体
ngx_int_t rc = ngx_http_discard_request_body(r);
if(rc != NGX_OK)
{
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "weijl rc=%d", rc);
return rc;
} /*设置返回的Content_Type。 注意,ngx_str_t有一个非常方便的初始化宏ngx_string,它能够把ngx_str_t的data和len成员都设置好*/
ngx_str_t type = ngx_string("text/plain");
//返回的包体内容
ngx_str_t response = ngx_string("Hello world! Here is the first Nginx HTTP program!");
ngx_str_t ress = ngx_string("<html>\r\n<head>\r\n<title>Welcome to nginx!</title>\r\n</head>\r\n<body bgcolor=\"white\" text=\"black\">\r\n<center><h1>Welcome to 192.168.0.7</h1></center>\r\n</body>\r\n</html>\r\n");
//设置返回状态码
r->headers_out.status = NGX_HTTP_OK;
//响应包是由包体内容的,须要设置Conten-Length长度
r->headers_out.content_length_n = response.len + ress.len;
//设置Content-Type
r->headers_out.content_type = type; //发送HTTP头部
rc = ngx_http_send_header(r);
if(rc == NGX_ERROR || rc > NGX_OK || r->header_only)
{
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "weijl rc=%d", rc);
return rc;
} //构造ngx_buf_t结构体准备发送包体
ngx_buf_t *b, *bs;
b = ngx_create_temp_buf(r->pool, response.len);
if(NULL == b)
{
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "weijl b=NULL");
return NGX_HTTP_INTERNAL_SERVER_ERROR;
} //将Hello World拷贝到ngx_buf_t指向的内存中
ngx_memcpy(b->pos, response.data, response.len);
//注意,一定要设置好last指针
b->last = b->pos + response.len;
//声明这是最后一块缓冲区
b->last_buf = 0; bs = ngx_create_temp_buf(r->pool, ress.len);
if(NULL == bs)
{
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "weijl bs=NULL");
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
//将ress拷贝到ngx_buf_t指向的内存中
ngx_memcpy(bs->pos, ress.data, ress.len);
//注意,一定要设置好last指针
bs->last = bs->pos + ress.len;
//声明这是最后一块缓冲区
bs->last_buf = 1; //构造发送时的ngx_chain_t结构体
ngx_chain_t out, outs;
out.buf = b;
//设置next为NULL
out.next = &outs; outs.buf = bs;
outs.next = NULL; ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "weijl 最后一步为发送包体。发送结束后HTTP框架会调用ngx_http_finalize_request方法结束请求\n"); //最后一步为发送包体。发送结束后HTTP框架会调用ngx_http_finalize_request方法结束请求
return ngx_http_output_filter(r, &out);
} //没有什么工作必须在HTTP框架初始化时完毕,不必实现ngx_http_module_t的8个回调方法
static ngx_http_module_t ngx_http_mytest_module_ctx =
{
NULL,
NULL, NULL,
NULL, NULL,
NULL, NULL,
NULL
}; //“mytest”配置项解析的回调方法
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;
} //mytest配置项的处理
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,//在出现配置项mytest时调用ngx_http_mytest解析
NGX_HTTP_LOC_CONF_OFFSET,
0,
NULL}, //很多其它的配置项能够在这里定义 ngx_null_command
}; //定义mytest模块
ngx_module_t ngx_http_mytest_module = {
NGX_MODULE_V1,
&ngx_http_mytest_module_ctx, /* module context */
ngx_http_mytest_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
};
将自己的HTTP模块代码编译进Nginx
在文件夹/home/weijl/workspace/nginx-1.10.3/src/http/下创建文件config:
weijl@weijl-ubuntu:~/workspace/nginx-1.10.3/src/http$ sudo touch config
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_module.c"
開始nginx的编译与又一次安装
进入Nginx源码文件夹/home/weijl/workspace/nginx-1.10.3下,依次运行例如以下命令:
weijl@weijl-ubuntu:~/workspace/nginx-1.10.3$ sudo ./configure --prefix=/usr/local/nginx --with-http_realip_module --with-http_sub_module --with-http_flv_module
--with-http_dav_module --with-http_gzip_static_module --with-http_stub_status_module --with-http_addition_module --with-pcre=/home/weijl/download/pcre-8.39 --with-openssl=/home/weijl/download/openssl-1.1.0e --with-http_ssl_module --with-zlib=/home/weijl/download/zlib-1.2.11
--add-module=/home/weijl/workspace/nginx-1.10.3/src/http
weijl@weijl-ubuntu:~/workspace/nginx-1.10.3$ sudo make
weijl@weijl-ubuntu:~/workspace/nginx-1.10.3$ sudo make install
開始測试验证
在浏览器中输入localhost:80/test,结果如图:
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvbGNsd2ps/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="">
基于Nginx实现一个自己的HTTP模块的更多相关文章
- 转: 构建基于Nginx的文件服务器思路与实现
在Web项目中使用独立的服务器来保存文件和图片的好处很多,如:便于统一管理,分流web服务器的压力,可进行访问加速等.另外当web服务器需要做集群进行负载均衡时,图片和文件上传在各个服务器之间同步将是 ...
- 基于Nginx dyups模块的站点动态上下线并实现简单服务治理
简介 今天主要讨论一下,对于分布式服务,站点如何平滑的上下线问题. 分布式服务 在分布式服务下,我们会用nginx做负载均衡, 业务站点访问某服务站点的时候, 统一走nginx, 然后nginx根据一 ...
- Tengine:基于Nginx的衍生版
engine是由淘宝网发起的Web服务器项目.它在Nginx的基础上,针对大访问量网站的需求,添加了很多高级功能和特性.Tengine的性能和稳定性已经在大型的网站如淘宝网,天猫商城等得到了很好的检验 ...
- 转: 基于nginx的hls直播系统
转自:http://blog.csdn.net/cjsafty/article/details/9108587 看点: 1. 详细解解答了 nginx rtmp配置过程. 前写了一篇基于nginx的h ...
- 基于nginx+lua+redis高性能api应用实践
基于nginx+lua+redis高性能api应用实践 前言 比较传统的服务端程序(PHP.FAST CGI等),大多都是通过每产生一个请求,都会有一个进程与之相对应,请求处理完毕后相关进程自动释放. ...
- Ubuntu 14.10下基于Nginx搭建mp4/flv流媒体服务器(可随意拖动)并支持RTMP/HLS协议(含转码工具)
Ubuntu 14.10下基于Nginx搭建mp4/flv流媒体服务器(可随意拖动)并支持RTMP/HLS协议(含转码工具) 最近因为项目关系,收朋友之托,想制作秀场网站,但是因为之前一直没有涉及到这 ...
- 基于nginx + lua实现的反向代理动态更新
大家都知道,nginx是当前应用非常广泛的web服务器,热度因为他的高并发高性能高可靠性,且轻量级!牛逼的不行,不多说这些. 今天要介绍的是,如何基于nginx和lua脚本,也就是在openresty ...
- 基于Nginx反向代理及负载均衡
基于Nginx反向代理及负载均衡 参考:http://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_pass 只要没有被启用,默认就是 ...
- 视频支持拖动进度条播放的实现(基于nginx)
http协议下的flv/mp4流式播放支持的三个要点: 1 服务器端要支持flv/mp4流式播放,现在nginx或者lighttpd都是支持这样的应用的,还支持mp4的流式播放(默认编译版本一般都是打 ...
随机推荐
- 扫描线三巨头 hdu1928&&hdu 1255 && hdu 1542 [POJ 1151]
学习链接:http://blog.csdn.net/lwt36/article/details/48908031 学习扫描线主要学习的是一种扫描的思想,后期可以求解很多问题. 扫描线求矩形周长并 hd ...
- 【找规律】【DFS】XVII Open Cup named after E.V. Pankratiev Stage 14, Grand Prix of Tatarstan, Sunday, April 2, 2017 Problem A. Arithmetic Derivative
假设一个数有n个质因子a1,a2,..,an,那么n'=Σ(a1*a2*...*an)/ai. 打个表出来,发现一个数x,如果x'=Kx,那么x一定由K个“基础因子”组成. 这些基础因子是2^2,3^ ...
- ajax 同步和异步的区别
举个例子:普通B/S模式(同步)AJAX技术(异步)同步:提交请求->等待服务器处理->处理完毕返回 这个期间客户端浏览器不能干任何事异步: 请求通过事件触发->服务器处理(这时浏览 ...
- [转]MySQL更改用户密码
grant all privilegeson *.* to root@'localhost'identified by 'root'with grant option; grant all privi ...
- HDU 4655 Cut Pieces(2013多校6 1001题 简单数学题)
Cut Pieces Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others)Total ...
- HDU 1507 Uncle Tom's Inherited Land*(二分匹配,输出任意一组解)
Uncle Tom's Inherited Land* Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- gdb signal 转
信号是一种软中断,是一种处理异步事件的方法.一般来说,操作系统都支持许多信号.尤其是UNIX,比较重要应用程序一般都会处理信号.UNIX定义了许 多信号,比如SIGINT表示中断字符信号,也就是Ctr ...
- VS2010 C++ 创建COM组件
1.项目中要使用到com组件,于是了解了一下com,并根据<C#高级编程>中关于com的介绍用vs创建了一下com,用于实验.以下均根据书中的demo做一遍,熟悉一下而已. 2.创建CoM ...
- Windows 10新增的6个快捷键:
Win+方向箭头:调整窗口贴边位置 Alt+Tab:切换窗口,按住不松时会有一个全新的界面方便你在不同的窗口间选择 Win+Tab:切换任务,这个松开后界面不会消失 Win+Ctrl+D:创建新的虚拟 ...
- Atlassian发布JIRA项目组合管理解决方案
在其年度用户峰会上,开发和协作软件供应商Atlassian发布了JIRA Portfolio,JIRA Portfolio是JIRA的一个附加组件"可以提供简单准确的视图用于计划和管理跨团队 ...