我的网站为什么打不开怎么回事,张家界企业网站制作,广西建设职业技术学校官方网站,网络平台怎么弄网上看到多篇先关文章#xff0c;觉得很不错#xff0c;这里合并记录一下#xff0c;仅供学习参考。
模块 nginx-auth-request-module 该模块是nginx一个安装模块#xff0c;使用配置都比较简单#xff0c;只要作用是实现权限控制拦截作用。默认高版本nginx#xff08;比…网上看到多篇先关文章觉得很不错这里合并记录一下仅供学习参考。
模块 nginx-auth-request-module 该模块是nginx一个安装模块使用配置都比较简单只要作用是实现权限控制拦截作用。默认高版本nginx比如1.12已经默认安装该模块下面介绍下使用该模块实现多个站点之间的统一权限控制。
例子1 这里用一个例子来说明下如下例子是包含site1(对应web1)、site2(对应web2)、auth(20.131:7001)在内的三个应用项目auth项目主要做权限拦截比如jwt校验等site1、site2分别为两个受保护的资源站点只有auth授权通过后才能访问该站点。
实现上述要求nginx配置详情如下nginx地址为20.198
upstream web1 {server 192.168.20.131:3000;
}upstream web2 {server 192.168.20.131:3001;
}
server {listen 80;server_name localhost;#charset koi8-r;#access_log /var/log/nginx/host.access.log main;location / {root /usr/share/nginx/html;index index.html index.htm;}location /api/web1 {auth_request /auth;error_page 401 error401;auth_request_set $user $upstream_http_x_forwarded_user;proxy_set_header X-Forwarded-User $user;proxy_pass http://web1;}location /api/web2 {auth_request /auth;error_page 401 error401;auth_request_set $user $upstream_http_x_forwarded_user;proxy_set_header X-Forwarded-User $user;proxy_pass http://web2;}location /auth {internal;proxy_set_header Host $host;proxy_pass_request_body off;proxy_set_header Content-Length ;proxy_pass http://192.168.20.131:7001/auth;}location error401 {add_header Set-Cookie NSREDIRECT$scheme://$http_host$request_uri;Path/;return 302 http://192.168.20.131:7001/login;}#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 /usr/share/nginx/html;}
} 配置好之后要明白一点那就是nginx-auth-request-module模块基本使用原理就是 1、auth_request对应的路由返回401 or 403时会拦截请求直接nginx返回前台401 or 403信息 2、auth_request对应的路由返回2xx状态码时不会拦截请求而是构建一个subrequest请求再去请求真实受保护资源的接口 所以基于此auth模块只需要校验然后返回相应的状态码即可实现权限拦截操作简单测试如下
auth代码
// 授权认证接口async auth() {console.log(Date.now());this.ctx.status 200;}// 失败后的登录页面async login() {console.log(失败了........);this.ctx.body {msg: 授权失败,code: 10001}} 这里的auth授权接口我们直接返回200login是上述auth项目下配置的路由用于授权失败后302至登录页面用的。 site1和site2代码相同只罗列一个如下
/* /api/web1/users如果是web2则为/api/web2/users */
router.all(/, function(req, res, next) {res.send(respond with a resource from web1);
});
这里只是简单渲染输出一个字符串而已测试如下
浏览器访问http://192.168.20.198/api/web1/users输出 改变auth接口如下
// 授权认证接口async auth() {console.log(Date.now());this.ctx.status 401;}// 失败后的登录页面async login() {console.log(失败了........);this.ctx.body {msg: 授权失败,code: 10001}}
这里将状态码改为了401再次访问http://192.168.20.198/api/web1/users输出 这里可以看到浏览器直接进行了302跳转因为鉴权失败直接重定向到登录页面了。 以上就是关于nginx-auth-request-module模块的基本操作及配置多个项目下部署统一的权限接口时还是相当有用的。 例子2 首先确保Nginx已经安装并启用了auth_request模块。然后编辑Nginx配置文件通常是nginx.conf或某个虚拟主机配置文件。
通过–with-http_auth_request_module添加auth_request模块
http { # 定义认证服务的逻辑 server { listen 127.0.0.1:8080; location /auth { # 此处为简单示例实际应用中应调用外部认证服务 if ($http_authorization Basic dXNlcm5hbWU6cGFzc3dvcmQ) { # 假设认证使用Basic Auth return 200; } return 401; } } server { listen 80; server_name example.com; location / { # 使用 auth_request 调用认证服务 auth_request /auth; # 处理认证服务的响应结果 error_page 401 error401; error_page 403 error403; # 正常处理请求 proxy_pass http://backend; } # 定义认证失败时的处理逻辑 location error401 { return 401 Unauthorized; } location error403 { return 403 Forbidden; } # 认证服务的代理设置 location /auth { proxy_pass http://127.0.0.1:8080/auth; proxy_pass_request_body off; # 不代理请求体到认证服务 proxy_set_header Content-Length ; proxy_set_header X-Original-URI $request_uri; } } }
配置说明
定义认证服务
server { listen 127.0.0.1:8080; location /auth { if ($http_authorization Basic dXNlcm5hbWU6cGFzc3dvcmQ) { return 200; } return 401; } } 这个server块模拟了一个简单的认证服务它监听127.0.0.1:8080根据请求头Authorization判断用户是否经过认证。在实际应用中这个应该是一个调用外部服务的代理配置。
主站点配置
server { listen 80; server_name example.com; location / { auth_request /auth; error_page 401 error401; error_page 403 error403; proxy_pass http://backend; } location error401 { return 401 Unauthorized; } location error403 { return 403 Forbidden; } location /auth { proxy_pass http://127.0.0.1:8080/auth; proxy_pass_request_body off; proxy_set_header Content-Length ; proxy_set_header X-Original-URI $request_uri; } }
auth_request /auth;该指令告诉Nginx在处理用户请求前先将请求发送到/auth进行认证。error_page 401 error401;和error_page 403 error403;定义认证失败时的处理逻辑将401或403错误重定向到相应的处理块。proxy_pass http://backend;成功认证后将请求代理到后端服务器。
认证失败处理
location error401 { return 401 Unauthorized; }
location error403 { return 403 Forbidden; } 认证失败时根据实际情况返回401或403状态码并附带相应的错误信息。
测试与验证 启动Nginx尝试访问http://example.com并使用不同的Authorization头部测试认证行为。如果头部包含正确的用户名和密码在本例中为Basic dXNlcm5hbWU6cGFzc3dvcmQ请求应被允许访问后端资源否则返回相应的错误状态码。 例子3
upstream web1 { server 192.168.20.131:3000; }
upstream web2 { server 192.168.20.131:3001; }
location ^~ /session/ { charset utf-8; auth_request /session-backend-info/; auth_request_set $backend $upstream_http_backend; proxy_set_header Forwarded $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Port $remote_port; proxy_set_header Host $http_host; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection $connection_upgrade; proxy_pass $backend/$request_uri; }
$backend为web1、web2 upstream