Nginx靜態(tài)資源或者路徑鑒權(quán)方式
更新時間:2024年06月19日 10:55:06 作者:longxiaobai_WJ
這篇文章主要介紹了Nginx靜態(tài)資源或者路徑鑒權(quán)方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
1. http_auth_request_module
# 版本說明 nginx 1.20.2, 默認自帶 http_auth_request_module # 模塊查看驗證 nginx -V (nginx -V| grep 'http_auth_request_module')
2. 鑒權(quán)接口準備
本文后端接口是通過Egg.js來實現(xiàn)的
實現(xiàn)代碼取決于項目中使用何種后端語言
'use strict';
const Controller = require('egg').Controller;
class AuthController extends Controller {
async index() {
const { ctx } = this;
ctx.body = 'hi, AuthController';
}
async login() {
const { ctx, service } = this;
const secrect = Date.now();
const jwt_token = await service.actionToken.apply(secrect);
ctx.helper.success();
// ctx.app.jwt.verify(jwt_token, this.app.config.jwt.secret);
ctx.body = {
status: 200,
secret: jwt_token,
};
}
async authorize() {
// 獲取 POST 傳遞的參數(shù)
console.log(this.ctx.params);
// 獲取 GET 傳遞的參數(shù)
console.log(this.ctx.query);
// 獲取通過 cookie 傳遞的參數(shù)
const isAuthToken = this.ctx.cookies.get('authorize', {
signed: false,
});
console.log('isAuthToken log', isAuthToken);
// 該代碼為兼容 4-2 配置代碼
console.log('x-original-uri', this.ctx.headers['x-original-uri']);
if (isAuthToken) {
this.ctx.body = {
status: 200,
secret: 'jwt_token',
};
} else {
this.ctx.status = 403;
}
}
}
module.exports = AuthController;3. 修改Nginx配置
server {
listen 8080;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location = / {
root index;
index index.html index.htm;
}
location /eggjs {
if ($http_cookie) {
set $code_cookie $http_cookie;
}
if ($arg_code) {
set $code_cookie "authorize=$arg_code; Path=/";
}
# 指定auth_request
auth_request /auth;
proxy_pass http://127.0.0.1:3000/static/index.html;
}
# 驗證配置
location /jwt {
proxy_pass http://localhost:3000/authorize;
}
location /scripts {
proxy_pass http://127.0.0.1:3000/static/scripts/;
}
location = /auth {
internal;
# $http_cookie
proxy_set_header Cookie "$code_cookie";
# 鑒權(quán)服務器的地址
proxy_pass http://localhost:3000/authorize;
# proxy_pass_request_body off;
# proxy_set_header Content-Length "";
# proxy_set_header X-Original-URI $query_string;
# proxy_set_header X-Original-URI $request_uri;
}
#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;
}
}4. 配置說明
指定auth_request
auth_request /auth;
傳遞完整的原始請求URI
- 由于身份驗證子請求將丟棄請求體,可以通過以下配置傳遞信息(本文未使用該方式,故皆已注釋);
# proxy_pass_request_body off; # proxy_set_header Content-Length ""; # proxy_set_header X-Original-URI $query_string; # proxy_set_header X-Original-URI $request_uri;
傳遞校驗信息
# URL: http://localhost:8080/eggjs?code=202304
# 設置變量 $code_cookie,以便后續(xù)使用,此處配置兼容 cookie 與 query 兩種寫法
if ($http_cookie) {
set $code_cookie $http_cookie;
}
if ($arg_code) {
# $arg_code 與 URL上的參數(shù)code相對應
# 即 secrect=202304 應對應 $arg_secrect
set $code_cookie "authorize=$arg_code; Path=/";
}
# 在此處使用變量 $code_cookie
proxy_set_header Cookie "$code_cookie";Extra Config Notes
以下配置,本文尚未驗證;
location /eggjs {
auth_request /auth;
auth_request_set $auth_status $upstream_status;
if ($auth_status = "403") {
return 403;
}
proxy_pass http://127.0.0.1:3000/static/index.html;
}總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
服務器報錯nginx?502?Bad?Gateway的原因及如何解決詳解
項目啟動時莫名其妙網(wǎng)站訪問不了,502 Bad Gateway,下面這篇文章主要給大家介紹了關(guān)于服務器報錯nginx?502?Bad?Gateway的原因及如何解決的相關(guān)資料,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2023-06-06
Windows安裝nginx1.10.1反向代理訪問IIS網(wǎng)站
這篇文章主要為大家詳細介紹了Windows安裝nginx1.10.1反向代理訪問IIS網(wǎng)站的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-11-11

