文章目录
一、前言
这里主要涉及模块安装及部分安全标签的设置,会伴随遇到的问题持续更新
二、模块内容
1、map 模块
源码安装:
这里主要是兼容做变量映射
适应范围: http、server、location
通常压缩目录下执行即可
① `./configure --add-module=src/http/modules/ngx_http_map_module`
② `make & make install`
作用相关
用途 | 描述 |
---|---|
变量转换 | 你可以使用map模块将一个变量的值映射到另一个变量,从而更改请求或响应中的变量值 |
HTTP状态码重定向 | 根据条件,你可以使用map模块来配置HTTP请求的状态码,实现URL重定向或自定义错误页面。 |
流量分发 | 你可以使用map模块来决定请求应该由哪个后端服务器或上游代理处理,根据条件选择后端服务器 |
URL重写 | map模块可以用于根据输入的URL来创建新的URL,用于请求的路由或URL重写 |
反向代理规则 | 可以使用map模块来定义反向代理规则,根据输入条件将请求代理到不同的上游服务器 |
2、lua 模块
如果建议还是放到脚本库去跑,用着用着,你会爱上它~
要合理利用工具搜刮合理的知识才是最好的。
2.1、nginx指定lua 指令
指令名称 | 作用域 | 备注 |
---|---|---|
———— | / | 访问阶段执行 Lua 代码 |
access_by_lua_block | loaction | 允许在访问阶段执行 Lua 代码 |
access_by_lua_file | loaction | 允许在访问阶段执行 Lua 代码 |
———— | / | 处理内容阶段执行 Lua 代码 |
content_by_lua_block | location | 块内执行lua脚本 |
content_by_lua_file | location | 指定一个 Lua 脚本文件执行 |
———— | / | 响应头阶段执行 Lua 代码 |
header_filter_by_lua_block | location | 在响应头过滤阶段执行 Lua 代码 |
header_filter_by_lua_file | location | 在响应头过滤阶段执行 Lua 代码 |
———— | / | 处理消息返回体阶段执行 Lua 代码 |
body_filter_by_lua_block | location | 在响应体过滤阶段执行 Lua 代码 |
body_filter_by_lua_file | location | 在响应体过滤阶段执行 Lua 代码 |
2.2、lua通用指令
指令名称 | 备注 |
---|---|
———— | 获取内容 |
ngx.req.get_headers() | 获取特定的请求头参数 |
ngx.req.get_uri_args() | 获取请求的 URI 参数 |
ngx.var.VARIABLE | 获取 Nginx 配置变量的值 |
ngx.time() | 当前的 Unix 时间戳 |
———— | 设置内容 |
ngx.exit(status) | 设置返回值,ngx.HTTP_OK、ngx.HTTP_NOT_FOUND、ngx.HTTP_FORBIDDEN |
ngx.header.HEADER | 设置响应头信息 |
ngx.print(content) | 将内容输出到响应体 |
ngx.say(content) | 将内容输出到响应体,末尾添加换行符 |
ngx.location.capture(“/”) | 后端服务发起额外的请求并获取其响应内容,可细查 |
ngx.exec(“/”) | 内部执行一个新请求,可细查 |
———— | 设置全局内容 |
ngx.shared.dict | 可以全局变量中设置内容,共享操作 |
源码安装:
# 下载 Lua 模块到特定目录,例如/opt/nginx
git clone https://github.com/openresty/lua-nginx-module.git
# 在nginx压缩目录下配置编译 Nginx,添加 Lua 模块
./configure --add-module=/opt/nginx/lua-nginx-module
make
sudo make install
命令安装:
#执行过程中,建议先暂停nginx
#1、 安装Lua解析器
# Ubuntu 或 Debian
sudo apt-get install luajit
# CentOS 或 Fedora
sudo yum install luajit
#2、安装相关模块集成依赖
# 这个过程中记得保存原本的config
sudo apt-get install -y curl gnupg2 ca-certificates lsb-release
echo "deb http://openresty.org/package/ubuntu $(lsb_release -sc) main" \
| sudo tee /etc/apt/sources.list.d/openresty.list
curl -fsSL https://openresty.org/package/pubkey.gpg | sudo apt-key add -
sudo apt-get update
sudo apt-get install openresty
#3、启动依赖必须
sudo systemctl start openresty.service
#4、编写脚本验证是否安装
location /lua {
default_type 'text/plain';
content_by_lua_block {
ngx.say("Hello, Lua!");
}
}
# 这时候记得去操作openresty下的nginx,而不是直接nginx
三、安全策略相关
1、csrf
1.1、referer 防护(仅可应对扫描)
一般来说都需要根据情况超过1个域名,根据情况处理。
这个部分首先是可以伪造的 ,通常只是用来过referer 扫描
① map 模块处理
http {
# 定义一个map映射配合多个domain 或ip
map $http_referer $is_valid_referer {
default 0; # 默认情况下,禁止请求
"~^https://(www\.)?domain1\.com" 1;
"~^https://(www\.)?domain2\.com" 1;
"~^https://(www\.)?domain3\.com" 1;
# 添加其他受信任的域名
}
server {
listen 80;
server_name domain1 domain2 domain3;
location / {
if ($is_valid_referer = 0) {
return 403; # 拒绝无效 Referer 头的请求
}
# 处理请求
}
}
}
② lua 模块处理
Tips: 这里实际资源目录是
/resources
,也可以根据需要,在相对部署位置放置
http {
server {
location / {
access_by_lua_block {
local referer = ngx.var.http_referer
-- 检查 Referer 是否存在且符合预期的来源
if referer and string.match(referer, "^https?://yourdomain.com") then
-- 在这里执行受保护资源的逻辑
ngx.exec("/resources")
else
ngx.status = ngx.HTTP_FORBIDDEN
ngx.say("Invalid Referer detected")
ngx.exit(ngx.HTTP_FORBIDDEN)
end
}
}
location /resources {
//
}
}
}
2、cors配置
跨站请求配置
属性名 | require | 描述内容 |
---|---|---|
Access-Control-Allow-Origin | yes | 允许的域名,只能填通配符或者单域名 |
Access-Control-Allow-Methods | yes | 允许跨域请求的 http 方法 |
Access-Control-Allow-Headers | yes | 返回支持的 http 请求头 |
Access-Control-Allow-Credentials | false | 标志着当前请求是否包含 cookies 信息,布尔值。只有一个可选值:true |
Access-Control-Max-Age | false | 以秒为单位的缓存时间,用于缓存预检请求 |
# 基础配置,可以根据需要,对应删除一些内容
add_header Access-Control-Allow-Origin *;
add_header Access-Control-Allow-Methods 'GET, POST, PUT, DELETE, OPTIONS';
add_header Access-Control-Allow-Credentials 'true';
add_header Access-Control-Allow-Headers 'Accept, Authorization,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
#这里是针对options的预检处理缓存,多少时间内免预检
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' *;
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Access-Control-Allow-Credentials' 'true';
add_header 'Access-Control-Allow-Methods' 'GET, POST, DELETE, PUT, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'Accept, Authorization,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
add_header 'Content-Type' 'text/plain charset=UTF-8';
add_header 'Content-Length' 0;
return 204;
}