xiaoxiao 发表于 2016-6-24 22:35:32

OneinStack编译云锁Nginx防护插件

本帖最后由 xiaoxiao 于 2016-11-16 10:16 编辑

编译安装

由于OneinStack使用的是Nginx1.10.2,而云锁现阶段支持Nginx最高版本为1.9.*
所以Oneinstack的用户需要自己编译Nginx,使云锁识别。具体编译执行步骤如下:

1.编译前先将已经安装的Nginx文件进行备份
cd /usr/local/nginx/sbin/
cp nginx nginx.bak



2:进入OneinStack的src目录,解压Nginx1.10.2压缩包
cd /root/oneinstack/src/
tar -zxvf nginx-1.10.2.tar.gz



3:修改ngx_http_upstream.c
备注:由于其不支持post过滤,所以需要修改nginx源码目录下ngx_http_upstream.c 文件,
在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(r)) {
return;
}

我们来实际操作下:
编辑ngx_http_upstream.c文件【约517行】
vi /root/oneinstack/src/nginx-1.10.2/src/http/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;
    }

例如下图:



编辑完后保存!


4:下载云锁防护插件压缩包
cd /root
wget https://codeload.github.com/yunsuo-open/nginx-plugin/zip/master -O nginx-plugin-master.zip
unzip nginx-plugin-master.zip
rm -rf nginx-plugin-master.zip




5:获取当前云锁插件源码所在目录的全路径
pwd



6:查看当前nginx加载的模块,在编译加载云锁防护模块的时候仍需加载这些模块
/usr/local/nginx/sbin/nginx -V



7:进行nginx源码目录,对nginx进行编译;编译时添加额外参数,
参数路径为第5步获取的云锁防护插件源码全路径“/root/nginx-plugin-master”;
cd oneinstack/src/nginx-1.10.2/
./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_stub_status_module --with-http_v2_module --with-http_ssl_module --with-ipv6 --with-http_gzip_static_module --with-http_realip_module --with-http_flv_module --with-openssl=../openssl-1.0.2h --with-pcre=../pcre-8.38 --with-pcre-jit --with-ld-opt='-ljemalloc' --add-module=/root/nginx-plugin-master




备注:1.8.0 以上要支持post过滤
在objs/Makefile文件中的CFLAGS后追加宏定义 HIGHERTHAN8
CFLAGS =-pipe-O -W -Wall -Wpointer-arith -Wno-unused-parameter -Werror -g -DHIGHERTHAN8

vi objs/Makefile




8:configure完成后进行make
make




9:将系统中原有的nginx用重新编译生成的nginx文件替换,替换后重启nginx使新编译nginx生效
rm -rf /usr/local/nginx/sbin/nginx
cp objs/nginx /usr/local/nginx/sbin/
service nginx restart





10.安装云锁,并让云锁识别新编译的nginx
备注:安装前先检查是否已关闭selinux,否则无法安装云锁;如已关闭则忽略此步骤
1)检查selinux状态
##如果现实参数为Disabled即为关闭状态,直接安装云锁
##如果显示参数为enabled即为开启状态,请执行(2)来关闭
getenforce
2)关闭selinux
修改/etc/sysconfig/selinux文件,将enforcing改为disabled,重启服务器。
vi /etc/sysconfig/selinux



确认完毕后,下面来正式安装云锁:
cd
wget http://download.yunsuo.com.cn/v3/yunsuo_agent_64bit.tar.gz
tar zxvf yunsuo_agent_64bit.tar.gz
rm -rf yunsuo_agent_64bit.tar.gz
cd yunsuo_install/
./install



查看是否正在运行,反之则在重装下:
ps -ef | grep yunsuo_agent




11:修改iptables,添加云锁所需的5555端口:
vi /etc/sysconfig/iptables

编辑参考:
*filter
:INPUT DROP
:FORWARD ACCEPT
:OUTPUT ACCEPT
:syn-flood -
-A INPUT -i lo -j ACCEPT
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 443 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 5555 -j ACCEPT
-A INPUT -p icmp -m limit --limit 100/sec --limit-burst 100 -j ACCEPT
-A INPUT -p icmp -m limit --limit 1/s --limit-burst 10 -j ACCEPT
-A INPUT -p tcp -m tcp --tcp-flags FIN,SYN,RST,ACK SYN -j syn-flood
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A syn-flood -p tcp -m limit --limit 3/sec --limit-burst 6 -j RETURN
-A syn-flood -j REJECT --reject-with icmp-port-unreachable
COMMIT

然后重启iptables:
service iptables restart
iptables -nL --line-number


最后编译nginx路径,使云锁识别:
cd /usr/local/yunsuo_agent/nginx/
./configure_compile_nginx /usr/local/nginx/




大功告成!







卸载插件

1.通过PC端卸载
将安装是备份的nginx.bak文件复制到云锁nginx目录的back下,覆盖原有nginx.bak文件。这样就可以在云锁PC端直接卸载了。
cd /usr/local/nginx/sbin/
rm -rf /usr/local/yunsuo_agent/nginx/backup/nginx.bak
cp nginx.bak /usr/local/yunsuo_agent/nginx/backup/



2.删除文件
手动删除或者重命名 /usr/local/yunsuo_agent/nginx/ 目录下的 libnginx_plugin.so(此操作需要关闭云锁自保护功能), 重启 nginx 服务即可。

3.使用原文件替换
手动使用系统原有的nginx直接替换当前使用的带有云锁插件的nginx。

FAQ

1.编译时可能出现的几种错误解决方法
① 遇如下错误信息 : cc1: all warnings being treated as errors, 编译器把警告信息作为错误处理了
    解决: 修改 objs/Makefile
    把CFLAGS = -pipe -O -W -Wall -Wpointer-arith-Wno-unused-parameter -Werror -g修改为:CFLAGS = -pipe -O -W -Wall -Wpointer-arith-Wno-unused-parameter -g即去掉-Werror选项
    重新make,注意是重新make而不是重新./configure
② 遇如下错误信息:undefined reference to `dlclose',由于编译器版本过高,需要在链接时,加入-ldl 选项
    解决: 修改 objs/Makefile
    搜索 -lpthread,定位到该行结束,加入 -ldl
    形如 -lpthread -lcrypt 修改为 -lpthread -lcrypt -ldl
    重新 make,注意是重新make而不是重新./configure




页: [1]
查看完整版本: OneinStack编译云锁Nginx防护插件