参数 | 说明 |
worker_connections | 每个进程的最大连接数 |
use | 工作进程数 |
/table>
code>http {
......
server {
listen 80; # 端口号
server_name localhost; # 域名
# 路径访问控制(默认访问路径,eg:/ ==> 根目录)
location / {
root /usr/share/nginx/html; # 网站根目录
index index.html index.htm index.py; # 首页配置
}
error_page 500 502 503 504 /50x.html; # 错误页面(可以自定义添404页面,error_page 404 /404.html;...)
# 访问xxx/50x.html的时候去指定目录找
location = /50x.html {
root /usr/share/nginx/html; # 错误页面所在路径
}
}
# 一个server配置一个虚拟 or 独立的站点(通过listen和server_name来区别多个server)
server {
......
}
}
/code>
code>[root@localhost dnt]# cat /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
[Install]
WantedBy=multi-user.target
/code>
code>[root@localhost dnt]# cat /usr/lib/systemd/system/nginx-debug.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStart=/usr/sbin/nginx-debug -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
[Install]
WantedBy=multi-user.target
/code>
li data-track="98">Nginx使用C/C++编写的,安装一下依赖:yum install gcc-c++ -y
/li>
li data-track="99">Nginx需要使用PCRE来进行正则解析:yum install pcre pcre-devel -y
/li>
li data-track="100">现在服务器和浏览器一般都是使用gzip:yum install -y zlib zlib-devel -y
/li>
li data-track="101">让Nginx支持https:yum install openssl openssl-devel -y
/li>
p data-track="109">PS:nginx -V
/p>
p data-track="111">PS:root权限编译哦~
/p>
code>./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'
/code>
p data-track="115">PS:提速:make -j 4 && make install
/p>
code># vi /usr/lib/systemd/system/nginx.service
[Unit]
Description=nginx - high performance web server
Documentation=http://nginx.org/en/docs/
After=network-online.target remote-fs.target nss-lookup.target
Wants=network-online.target
[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/bin/kill -s HUP $MAINPID
ExecStop=/bin/kill -s TERM $MAINPID
[Install]
WantedBy=multi-user.target
/code>
p data-track="122">PS:规则生效:firewall-cmd --reload
/p>
strong>nginx: [emerg] getpwnam("nginx") failed
/strong>的错误可以参考我写这篇文章:https://www.cnblogs.com/dotnetcrazy/p/11304783.html
p data-track="125">PS:核心:useradd -s /sbin/nologin -M nginx
/p>
strong>默认是不支持Lua的
/strong>,所以需要自己编译安装下
p data-track="129">PS:记得安装下Lua库:yum install lua lua-devel -y
/p>
li data-track="131">安装
Lua即时编译器:LuaJIT目前最新:http://luajit.org/download/LuaJIT-2.0.5.tar.gz
/li>
li data-track="132">安装Nginx模块:ngx_devel_kit and lua-nginx-modulengx_devel_kit:https://github.com/simplresty/ngx_devel_kit/archive/v0.3.1.tar.gzlua-nginx-module:https://github.com/openresty/lua-nginx-module/archive/v0.10.15.tar.gz
/li>
li data-track="133">重新编译Nginx:复制在线安装的
编译参数(nginx -V)然后
添加两个参数--add-module=../ngx_devel_kit-0.3.1--add-module=../lua-nginx-module-0.10.15
/li>
code># 编译安装
make install PREFIX=/usr/local/LuaJIT
# 导入环境变量
export LUAJIT_LIB=/usr/local/LuaJIT/lib
export LUAJIT_INC=/usr/local/LuaJIT/include/luajit-2.0
/code>
p data-track="139">echo "/usr/local/LuaJIT/lib" >> /etc/ld.so.conf
/p>
code>./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' --add-module=../ngx_devel_kit-0.3.1 --add-module=../lua-nginx-module-0.10.15
/code>
p data-track="149">PS:vi /etc/nginx/nginx.conf
/p>
code>server {
listen 80;
server_name localhost;
charset utf-8; # 默认编码为utf-8
location / {
root html;
index index.html index.htm;
}
...
# 测试Nginx的Lua(添加这一段)
location /hello {
default_type 'text/plain';
content_by_lua 'ngx.say("欢迎访问逸鹏说道公众号~")';
}
...
}
/code>
p data-track="152">PS:配置生效:nginx -s reload -c /etc/nginx/nginx.conf
/p>
code># 获取客户端ip
location /myip {
default_type 'text/plain';
content_by_lua '
clientIP = ngx.req.get_headers()["x_forwarded_for"]
ngx.say("IP:",clientIP)
';
}
/code>
p data-track="158">https://github.com/loveshell/ngx_lua_waf
/p>
li data-track="159">拦截Cookie类型工具
/li>
li data-track="160">拦截异常post请求
/li>
li data-track="161">拦截CC洪水攻击
/li>
li data-track="162">拦截URL
/li>
li data-track="163">拦截arg(提交的参数)
/li>
li data-track="167">args里面的规则get参数进行过滤的
/li>
li data-track="168">url是只在get请求url过滤的规则
/li>
li data-track="169">post是只在post请求过滤的规则
/li>
li data-track="170">whitelist是白名单,里面的url匹配到不做过滤
/li>
li data-track="171">user-agent是对user-agent的过滤规则
/li>
p data-track="175">参数简单说明下:
红色字体部分需要修改/p>
code>lua_package_path "/etc/nginx/waf/?.lua";
lua_shared_dict limit 10m;
init_by_lua_file /etc/nginx/waf/init.lua;
access_by_lua_file /etc/nginx/waf/waf.lua;
/code>
p data-track="180">PS:不重启的方式加载配置:Nginx -s reload -c /etc/nginx/nginx.conf
/p>
p data-track="183">过滤规则在wafconf下,可根据需求自行调整,每条规则需换行,或者用|分割
/p>
p data-track="185">PS:默认规则没有这点的防护
/p>
p data-track="187">PS:如果是从post进行注入,或者cookie中转注入,那么在对应规则里面添加就行,我这边只是演示下防火墙被绕过该怎么解决~(多看看日志)
/p>
strong>使用ab来测试下nginx+lua的waf对cc的防御效果
/strong>
p data-track="191">PS:测试前curl http://192.168.0.10/hello 看看返回内容,测试后再curl看看返回内容
/p>
p data-track="194">PS:其他配置今天就不详细讲解了,下次讲Nginx的时候会说的
/p>
p data-track="197">http下添加:server_tokens off;
/p>
检查下语法:nginx -t
不重启的方式加载配置文件:nginx -s reload
现在效果:
出处:https://www.cnblogs.com/dotnetcrazy
策略思维与细节的完美结合,有效帮您减少试错成本
策略思维与细节的完美结合,有效帮您减少试错成本
立即与安优联系,开启网站设计全新体验
立即与安优联系,开启网站设计全新体验
立即与安优联系
开启网站设计全新体验
立即与安优联系
开启网站设计全新体验