http://nginx.org/ NGINX官网
创建文件夹
mkdir /software
cd /software
下载当前最新的稳定版
wget https://nginx.org/download/nginx-1.18.0.tar.gz
安装必要插件
yum -y install gcc pcre pcre-devel zlib zlib-devel openssl openssl-devel
简介:
gcc 它可以编译 C,C++,Ada,Object C和Java等语言
pcre pcre-devel pcre是一个perl库,包括perl兼容的正则表达式库,nginx的http模块使用pcre来解析正则表达式,所以需要安装pcre库
zlib zlib-devel zlib库提供了很多种压缩和解压缩方式nginx使用zlib对http包的内容进行gzip,所以需要安装
openssl openssl-devel openssl是web安全通信的基石,没有openssl,可以说我们的信息都是在裸奔
tar -zxvf nginx-1.18.0.tar.gz # 解压
mv nginx-1.18.0 nginx
cd nginx
# 下载 naxsi
cd /software
git clone https://github.com/nbs-system/naxsi.git
# 安装
./configure --prefix=/software/nginx --user=nginx --group=nginx --with-http_ssl_module --with-http_stub_status_module --with-http_gzip_static_module --with-file-aio --with-http_dav_module --add-module=../naxsi/naxsi_src/
#指定安装路径 --prefix=/software/nginx
make #编译
make install #安装
nginx 配置文件中,user nginx nginx 可以修改启动用户。
引用 naxsi 文件
在nginx主配置文件中引入naxsi核心规则文件,这里要放在http里面
cp /software/naxsi/naxsi_config/naxsi_core.rules /software/nginx/conf/
修改 nginx 的配置文件:
http {
include /software/nginx/conf/naxsi_core.rules; # Naxsi 引用核心规则
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
SecRulesEnabled; #enable naxsi 启用 naxsi
# LearningMode; #enable learning mode # 是否启用学习模式,只记录,不拦截,方便自己设置白名单
LibInjectionSql; #enable libinjection support for SQLI
LibInjectionXss; #enable libinjection support for XSS
DeniedUrl "/stop.html"; # 触发规则以后显示的页面
CheckRule "$SQL >= 8" BLOCK; #the action to take when the $SQL score is superior or equal to 8
CheckRule "$RFI >= 8" BLOCK;
CheckRule "$TRAVERSAL >= 5" BLOCK;
CheckRule "$UPLOAD >= 5" BLOCK;
CheckRule "$XSS >= 8" BLOCK;
root html;
index index.html index.htm;
}
}
}
测试一下,是否生效:
/?ac=digg&ac2=&id=1&tab=vod union select null,md5(1231412414) 拦截原因:防止SQL联合查询,可疑内容:Union
/?ss=x and 1=1,拦截原因:防止简单的and or 方式注入
/?ss=information_schema。。。,拦截原因:非法访问information_schema数据库
/?typeArr[1' or `@\'`=1 and (SELECT 1 FROM (select count(*),concat(floor(rand(0)*2),(substring((Select pwd from dede_admin limit 1,1),1,62)))a from information_schema.tables group by a)b) and ']=11&&kwtype=0&q=1111&searchtype=title,拦截原因:防止对数据库进行数据查询操作,可疑内容:Select pwd from dede_admin limit
/?typeArr[1' or `@\'`=1 and (SELECT 1 FROM (select count(*),concat(floor(rand(0)*2),(substring((Select uname from dede_admin limit 1,1),1,62)))a from information_schema.tables group by a)b) and ']=11&&kwtype=0&q=1111&searchtype=title,拦截原因:防止对数据库进行数据查询操作,可疑内容:Select uname from dede_admin limit
/?aid=1&_FILES[type][name]&_FILES[type][size]&_FILES[type][type]&_FILES[type][tmp_name]=aa\'and char(@`'`) Union*/ SeLect*/ 1,2,3,group_concat(userid,0x23,pwd),5,6,7,8,9 from `#@__admin`#,拦截原因:防止SQL联合查询,可疑内容:Union SeLect 1,2,3,group_concat(userid,0x23,pwd),5,6,7,8,9 from `#@__admin`#
备注:
进入到安装nginx目录下面的sbin
启动命令
./nginx
从容停止服务
这种方法较stop相比就比较温和一些了,需要进程完成当前工作后再停止。
nginx -s quit
立即停止服务
这种方法比较强硬,无论进程是否在工作,都直接停止进程。
nginx -s stop
查询nginx主进程号
ps -ef | grep nginx
从容停止 kill -QUIT 主进程号
快速停止 kill -TERM 主进程号
强制停止 kill -9 nginx
运行 ./nginx 提示 [emerg]: getpwnam(“nginx”) failed
useradd -s /sbin/nologin -M nginx
id nginx
报错
在执行 nginx -s reload 的时候报错
nginx: [error] invalid PID number "" in "/run/nginx.pid"
解决方法
./nginx -c /software/nginx/conf/nginx.conf
nginx.conf文件的路径可以从nginx -t的返回中找到。
加入系统服务
如果用yum install命令安装的,yum命令会自动创建nginx.service文件,直接用命令systemcel enable nginx.service设置开机启动即可。源码安装的手动建立nginx.service服务文件。
在 /usr/lib/systemd/system 中创建 nginx.service 文件
[Unit]
Description=nginx
After=network.target
[Service]
Type=forking
ExecStart=/software/nginx/sbin/nginx -c /software/nginx/conf/nginx.conf
ExecReload=/software/nginx/sbin/nginx -s reload
ExecStop=/software/nginx/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
然后执行
sudo systemctl enable nginx
参考:
https://www.cnblogs.com/shiyuelp/p/11945882.html
https://blog.csdn.net/qq_40016949/article/details/81088802
https://my.oschina.net/u/4641386/blog/4552757
https://nginx.org/en/download.html
https://blog.csdn.net/achang21/article/details/80039561
https://blog.csdn.net/u010533511/article/details/79932060[重新编译替换nginx]