nginx子请求拦截鉴权

43 阅读1分钟

1. 是否有安装权限校验模块

如果已经安装了的话走第三步

nginx -V 2>&1 | grep -- 'http_auth_request_module'
# 提示有以下信息即可 否则输出空字符串 执行步骤2
configure arguments: --with-http_auth_request_module

2. 安装权限子模块

# 重新编译nginx
./configure --with-http_auth_request_module
# 安装
make
sudo make install
# 查看是否安装
configure arguments: --with-http_auth_request_module

3. 修改nginx配置文件

location ^~ /images/ {
    auth_request /auth-proxy;
    auth_request_set $auth_status $upstream_status;
    proxy_set_header Authorization $http_authorization;  
    alias /root/upload/images/;	 
	error_page 401 /404.html;
}

location /auth-proxy {
    internal;
    proxy_pass http://domain.com/api/info;  # 将请求转发给后端服务器

    proxy_set_header X-Original-URI $request_uri;  # 可选,用于传递原始 URI
    proxy_set_header X-Requested-With XMLHttpRequest;  # 可选,用于指示异步请求
    # 配置其他参数
    # 将所有请求头全部传递给后端服务
    proxy_set_header Host $host;
    proxy_set_header Authorization $http_authorization;
}