Nginx靜態(tài)資源或者路徑鑒權(quán)方式
1. http_auth_request_module
# 版本說明 nginx 1.20.2, 默認(rèn)自帶 http_auth_request_module # 模塊查看驗(yàn)證 nginx -V (nginx -V| grep 'http_auth_request_module')
2. 鑒權(quán)接口準(zhǔn)備
本文后端接口是通過Egg.js
來實(shí)現(xiàn)的
實(shí)現(xiàn)代碼取決于項(xiàng)目中使用何種后端語言
'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; } # 驗(yàn)證配置 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)服務(wù)器的地址 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;
傳遞完整的原始請(qǐng)求URI
- 由于身份驗(yàn)證子請(qǐng)求將丟棄請(qǐng)求體,可以通過以下配置傳遞信息(本文未使用該方式,故皆已注釋);
# 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;
傳遞校驗(yàn)信息
# URL: http://localhost:8080/eggjs?code=202304 # 設(shè)置變量 $code_cookie,以便后續(xù)使用,此處配置兼容 cookie 與 query 兩種寫法 if ($http_cookie) { set $code_cookie $http_cookie; } if ($arg_code) { # $arg_code 與 URL上的參數(shù)code相對(duì)應(yīng) # 即 secrect=202304 應(yīng)對(duì)應(yīng) $arg_secrect set $code_cookie "authorize=$arg_code; Path=/"; } # 在此處使用變量 $code_cookie proxy_set_header Cookie "$code_cookie";
Extra Config Notes
以下配置,本文尚未驗(yàn)證;
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é)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Nginx配置如何區(qū)分PC或手機(jī)訪問不同域名
這篇文章主要介紹了Nginx配置如何區(qū)分PC或手機(jī)訪問不同域名,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-10-10nginx支持.htaccess文件實(shí)現(xiàn)偽靜態(tài)的方法分享
這篇文章主要介紹了nginx支持.htaccess文件實(shí)現(xiàn)偽靜態(tài)的方法分享,需要的朋友可以參考下2015-01-01nginx配置多個(gè)前端項(xiàng)目實(shí)現(xiàn)步驟
本文主要介紹了nginx配置多個(gè)前端項(xiàng)目實(shí)現(xiàn)步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03nginx中的兩個(gè)模塊的proxy_pass的區(qū)別解析
在nginx中配置proxy_pass代理轉(zhuǎn)發(fā)時(shí),如果在proxy_pass后面的url加/,表示絕對(duì)根路徑;如果沒有/,表示相對(duì)路徑,把匹配的路徑部分也給代理走。本文給大家介紹nginx中的兩個(gè)模塊的proxy_pass的區(qū)別,感興趣的朋友一起看看吧2021-11-11nginx配置多個(gè)虛擬主機(jī)vhost的方法示例
這篇文章主要介紹了nginx配置多個(gè)虛擬主機(jī)vhost的方法示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-10-10nginx啟動(dòng)、配置及測(cè)試圖文詳解(全網(wǎng)最全)
nginx是一個(gè)輕量級(jí)的網(wǎng)頁服務(wù)器、方向代理服務(wù)器和電子郵件代理服務(wù)器,具有配置靈活、靜態(tài)資源高并發(fā)、系統(tǒng)資源占用少、擁有緩存服務(wù)等優(yōu)點(diǎn),這篇文章主要給大家介紹了關(guān)于nginx啟動(dòng)、配置及測(cè)試的相關(guān)資料,需要的朋友可以參考下2024-02-02Nginx 代理轉(zhuǎn)發(fā)阿里云OSS上傳的實(shí)現(xiàn)代碼
這篇文章主要介紹了Nginx 代理轉(zhuǎn)發(fā)阿里云OSS上傳的實(shí)現(xiàn)代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-09-09Nginx服務(wù)器中配置非80端口的端口轉(zhuǎn)發(fā)方法詳解
這篇文章主要介紹了Nginx服務(wù)器中配置非80端口的端口轉(zhuǎn)發(fā)方法詳解,文中使用到了Nginx中的proxy_pass配置項(xiàng),需要的朋友可以參考下2016-04-04