@nginx自编译
适配Nginx版本,是使用已经预编译好的包含防护模块的Nginx替换掉您当前系统中使用的Nginx。但当您的Nginx含有以下参数时,需编译安装。
--add-module、--with-file-aio、--with-http_xslt_module、--with-http_image_filter_module、--with-http_geoip_module、--with-http_perl_module、--with-perl_modules_path、--with-perl、--with-google_perftools_module、--add-dynamic-module、--with-stream=dynamic
建议:在安装防护模块前先重启一次Nginx的服务,确保业务重启后可以正常运行,然后再安装nginx防护模块。
-
编译前先将已经安装的Nginx文件进行备份,通过ps命令查看nginx文件的路径,将nginx可执行文件备份为nginx.bak。以下所有步骤都以自身nginx路径为准。
ps -elf | grep nginx
# cd /usr/local/nginx/sbin/
# cp nginx nginx.bak
- 通过步骤1查看的路径跟-v参数查看当前Nginx版本,如有源码包则忽略此步骤。无源码包则到Nginx官网下载与当前系统Nginx大版本匹配的源码包。(假设当前Nginx版本为1.10)
/usr/local/nginx/sbin/ -v
# wget http://nginx.org/download/nginx-1.10.1.tar.gz
4.由于其不支持post过滤,所以需要修改Nginx文件。1.8.0 版本以下修改源码目录下ngx_http_upstream.c文件。1.8.0 及以上版本和Tengine跳过该步骤。
在static void ngx_http_upstream_init_request(ngx_http_request_t *r);行上方添加:int ngx_http_yunsuo_post_in_handler(ngx_http_request_t *r);和在ngx_http_upstream_init_request后,添加:if (ngx_http_yunsuo_post_in_handler®) {
return;
}
# cd nginx-1.10.1/src/http/
# vim ngx_http_upstream.c
--------------------下面这段是添加的----------------
int
ngx_http_yunsuo_post_in_handler(ngx_http_request_t *r);
-----------------------------------------------
static void
ngx_http_upstream_init_request(ngx_http_request_t *r)
{
ngx_str_t *host;
ngx_uint_t i;
ngx_resolver_ctx_t *ctx, temp;
ngx_http_cleanup_t *cln;
ngx_http_upstream_t *u;
ngx_http_core_loc_conf_t *clcf;
ngx_http_upstream_srv_conf_t *uscf, **uscfp;
ngx_http_upstream_main_conf_t *umcf;
--------------------下面这段是添加的----------------
if (ngx_http_yunsuo_post_in_handler(r)) {
return;
}
------------------------------------------------
if (r->aio) {
return;
}
![在这里插入图片描述](https://img
-blog.csdnimg.cn/20181211172342296.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzM2MzY1NTIw,size_16,color_FFFFFF,t_70)
5.下载护模块压缩包
如下载失败:请联系博主索取模块
# cd /root/
# wget https://codeload.github.com/yunsuo-open/nginx-plugin/zip/master -O nginx-plugin-master.zip
6.解压防护模块压缩包nginx-plugin-master.zip
# unzip nginx-plugin-master.zip
7.获取当前防护模块所在目录的全路径
# cd nginx-plugin-master/
# pwd
8.查看当前nginx加载的模块,在编译加载防护模块的时候仍需加载这些模块
# /usr/local/nginx/sbin/nginx –V
9.进入nginx源码目录,对nginx进行编译;编译时添加防护模块参数,参数路径为第7步获取的防护模块源码全路径“/root/nginx-plugin-master”
# cd nginx-1.10.1/
# ./configure --prefix=/usr/local/nginx --with... --add-module=/root/nginx-plugin-master
10. 1.8.0 及以上要支持post过滤,则在objs/Makefile文件中的CFLAGS后追加宏定义 -DHIGHERTHAN8。Tengine跳过该步骤。
# vim objs/Makefile
CFLAGS = -pipe -O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -DHIGHERTHAN8
11.configure完成后进行make
(如原本无nginx,make后还需make install)
# make
12.将系统中原有的nginx用重新编译生成的nginx文件替换,替换后重启nginx使新编译nginx生效
# rm -rf /usr/local/nginx/sbin/nginx
# cp objs/nginx /usr/local/nginx/sbin/
# service nginx restart
13.到此通过PC端连接到服务器端,在PC端的界面上可以看到已识别nginx插件。
14.卸载nginx防护模块
只需要把原有的nginx脚本替换回去重启nginx就可以。
FAQ
nginx编译报错-1.6.3、2 动态链接库报错
1、代理 nginx自编译需请留言联系、、、