登录
首页 >  文章 >  linux

ubuntu Install nginx Lua and configure waf

时间:2025-02-03 19:31:18 244浏览 收藏

大家好,我们又见面了啊~本文《ubuntu Install nginx Lua and configure waf》的内容中将会涉及到等等。如果你正在学习文章相关知识,欢迎关注我,以后会给大家带来更多文章相关文章,希望我们能一起进步!下面就开始本文的正式内容~

ubuntu Install nginx Lua and configure waf

一、安装ngx_devel_kit

cd /opt
git clone https://github.com/simplresty/ngx_devel_kit.git && cd ngx_devel_kit && git checkout master

二、安装lua-nginx-module

cd /opt
git clone https://github.com/openresty/lua-nginx-module.git && cd lua-nginx-module && git checkout master

三、安装Lua环境 (Luajit)

cd /opt
git clone http://luajit.org/git/luajit-2.0.git && cd luajit-2.0 && git pull
make && make install

四、设置Lua环境变量

cd /opt/nginx-1.12.2  # 请替换为你的Nginx安装路径
export LUAJIT_LIB=/usr/local/lib
export LUAJIT_INC=/usr/local/include/luajit-2.0

五、编译Nginx (假设你已完成Nginx的编译安装)

cd /opt/nginx-1.12.2/ # 请替换为你的Nginx安装路径
./configure --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_addition_module --with-http_dav_module --with-http_geoip_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module --with-http_v2_module --with-http_sub_module --with-http_xslt_module --with-stream --with-stream_ssl_module --with-mail --with-mail_ssl_module --with-threads --with-pcre=/opt/pcre-8.38 --with-zlib=/opt/zlib-1.2.11 --with-openssl=/opt/openssl-1.0.2n --add-module=/opt/ngx_devel_kit --add-module=/opt/lua-nginx-module --with-ld-opt=-Wl,-rpath,$LUAJIT_LIB
make
mv /usr/sbin/nginx /usr/sbin/nginx.old #备份旧的nginx
cp objs/nginx /usr/sbin/nginx # 复制新的nginx可执行文件
# 确保nginx -t 命令指向正确的nginx可执行文件路径 (例如,修改Makefile中的路径)
make upgrade

六、安装并配置ngx_lua_waf

cd /etc/nginx/
git clone https://github.com/loveshell/ngx_lua_waf.git && cd ngx_lua_waf && git checkout master

# 编辑nginx.conf文件,在http块中添加以下配置:
vim /etc/nginx/nginx.conf
http {
    ...
    lua_package_path "/etc/nginx/ngx_lua_waf/?.lua";
    lua_shared_dict limit 10m;
    init_by_lua_file /etc/nginx/ngx_lua_waf/init.lua;
    access_by_lua_file /etc/nginx/ngx_lua_waf/waf.lua;
    ...
}

# 配置waf规则文件
vim /etc/nginx/ngx_lua_waf/config.lua
RulePath = "/etc/nginx/ngx_lua_waf/wafconf/"  --规则存放目录
attacklog = "on"  --是否开启攻击信息记录,需要配置logdir
logdir = "/home/wwwlogs/"  --log存储目录,需要nginx用户的可写权限。日志文件名称格式如下:虚拟主机名_sec.log
UrlDeny = "on"  --是否拦截url访问
Redirect = "on"  --是否拦截后重定向
CookieMatch = "on"  --是否拦截cookie攻击
postMatch = "on"  --是否拦截post攻击
whiteModule = "on"  --是否开启URL白名单
black_fileExt = {"php","jsp"}  --填写不允许上传文件后缀类型
ipWhitelist = {"127.0.0.1"}  --ip白名单,多个ip用逗号分隔
ipBlocklist = {"1.0.0.1"}  --ip黑名单,多个ip用逗号分隔
CCDeny = "on"  --是否开启拦截cc攻击(需要nginx.conf的http段增加lua_shared_dict limit 10m;)
CCrate = "100/60"  --设置cc攻击频率,单位为秒. 默认1分钟同一个IP只能请求同一个地址100次
html = [[Please go away~~]]  --警告内容,可在中括号内自定义

请记得将 /opt/nginx-1.12.2/opt/pcre-8.38/opt/zlib-1.2.11/opt/openssl-1.0.2n 替换成你实际的安装路径。 完成配置后,重启Nginx使其生效。 /home/wwwlogs/ 目录需要提前创建并赋予nginx用户写入权限。 请根据实际情况修改 config.lua 文件中的配置。

今天关于《ubuntu Install nginx Lua and configure waf》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!

相关阅读
更多>
最新阅读
更多>
课程推荐
更多>