自定义nginx启动
右键/以管理员身份运行

切换到nginx路径/运行nginx
cd /hhh-nginx-bin-1.24.0
./nginx

查看是否运动成功

hello world
nginx 安装njs 模块
yum install nginx-module-njs
引用 njs 模块
// nginx.conf
load_module modules/ngx_http_js_module.so;
events {}
http {
server {}
}
运行
// nginx.conf
load_module modules/ngx_http_js_module.so;
events {}
http {
js_path "/etc/nginx/njs/";
js_import index.js;
server {
listen 80;
location / {
js_content index.hello;
}
}
}
// /etc/nginx/njs
function hello(r) {
r.return(200, "Hello world, index!");
}
export default { hello };


授权认证
后端项目
- 监听端口为
8080
nginx.conf
http {
#引入njs项目
js_path "./../njs/";
#引入njs中导出的js模块
js_import main from http/authorization/auth_request.js;
server {
#nginx起一个端口
listen 8001;
# 过滤所有8001下所有的请求,如需监听特定的路径,如请求/secure/{xxx},则形式为`location /secure/`
location / {
# 发送一个子请求
auth_request /validate;
# 代理8080端口(后端项目)
proxy_pass http://127.0.0.1:8080;
}
# 处理子请求
location /validate {
# 只允许国内访问
internal;
# 自定义返回内容
js_content main.authorize;
}
}
}
njs
http/authorization/auth_request.js
function authorize(r) {
var Authorization = r.headersIn.Authorization;
if (!Authorization) {
r.error("没有对应的请求头,请配置");
r.return(401);
return;
}
r.return(200);
}
export default { authorize };
结果
没有对应的请求头

有对应的请求头

**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。