現在很多基於百度的nginx 防止sql注入都是get方式,如果post就沒有了.
坑點:
1.$query_string 獲取get請求的數據
2.$request_body 獲取post請求的數據,但是這里如果對$request_body進行校驗,則為空!!!!!!!!!! 所以這個方式不可行.
3.在網上找到,通過另外一種方式來獲取請求數據.openresty.下面就來說一說如何操作.
1.環境:
1.1 操作系統 windows 10
1.2 http://openresty.org/en/download.html 下載可以獲取請求參數基於windows下的nginx(模塊自帶nginx)

2.編寫 nginx.conf 配置文件(./openresty-1.19.3.1-win64/conf/nginx.conf)

下面是原文
server {
#nginx 監聽端口
listen 80;
#nginx 服務名稱
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
#root html;
#index index.html index.htm;
default_type text/html;
#打開 lua 允許使用這種方式來獲取請求數據
lua_need_request_body on;
access_by_lua_block {
#獲取 post請求數據
local body = ngx.var.request_body
#獲取 get請求數據
local query = ngx.var.query_string
#正則表達式
local regex = "(.*?((select)|(from)|(count)|(delete)|(update)|(drop)|(truncate)).*?){1,}"
#匹配post參數
local m,err = ngx.re.match(body, regex)
#匹配get參數
local n,err = ngx.re.match(query, regex)
#做get或者post校驗
if m then
#返回數據 (二選一)
ngx.say('{"code": 999,"msg": "傳參異常","ok": false,"runningTime": "0ms"}')
#返回頁面
#ngx.exit(404)
end
}
#設置日志
access_log logs/host.access.log json_log;
#轉換應用服務ip和端口
proxy_pass http://127.0.0.1:8081;
}
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
效果:



希望大家互相學習.
