【Nginx】基于http响应状态码做权限控制的auth_request模块

  • auth_request模块的用途

  • 安装auth_request模块(默认并未编译进Nginx,通过--with-http-auth-request-module启用)
    cd nginx-1.8.0
    ./configure \
    --prefix=/usr/local/nginx \
    --pid-path=/var/run/nginx/nginx.pid \
    --lock-path=/var/lock/nginx.lock \
    --error-log-path=/var/log/nginx/error.log \
    --http-log-path=/var/log/nginx/access.log \
    --with-http_gzip_static_module \
    --with-http_auth_request_module \
    --http-client-body-temp-path=/var/temp/nginx/client \
    --http-proxy-temp-path=/var/temp/nginx/proxy \
    --http-fastcgi-temp-path=/var/temp/nginx/fastcgi \
    --http-uwsgi-temp-path=/var/temp/nginx/uwsgi \
    --http-scgi-temp-path=/var/temp/nginx/scgi

  • 样例
    location / {
        root   html;
        index  index.html index.htm;
        auth_request /auth;
    }
    location /auth {
            #return 403 "error";
            return 200 "success"
    }

  • 实际应用中,/auth中一般通过反向代理到鉴权服务器来返回状态码判断是否通过验证
    location /private/ {
          auth_request /auth;
          #隐藏请求头中的请求参数
           proxy_hide_header appid;
           proxy_hide_header timestamp;
           proxy_hide_header sign;
           proxy_pass http://127.0.0.1:8080/业务接口地址;
    }
    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-URL $request_uri;
    
           #以下待研究内容
           # internal;
           # proxy_set_header Host $host;
           # proxy_no_cache "1";
    }
    
    ​

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要在Nginx中添加http_auth_request_module模块,你可以按照以下步骤操作: 1. 确认你的Nginx版本是否支持http_auth_request_module模块。你可以使用以下命令来检查: ``` nginx -V ``` 在输出结果中查找是否包含 "--with-http_auth_request_module" 字样,如果有,说明该模块已经被支持。 2. 如果你的Nginx版本不支持该模块,你需要重新编译Nginx并启用该模块。首先,下载Nginx的源代码: ``` wget http://nginx.org/download/nginx-x.x.x.tar.gz ``` (将 "x.x.x" 替换为你想要下载的版本号) 3. 解压源代码包: ``` tar -zxvf nginx-x.x.x.tar.gz ``` 4. 进入解压后的目录: ``` cd nginx-x.x.x ``` 5. 执行以下命令进行配置,确保启用了http_auth_request_module模块: ``` ./configure --with-http_auth_request_module ``` 6. 执行以下命令编译并安装Nginx: ``` make sudo make install ``` 7. 配置Nginx的配置文件(通常是位于 /etc/nginx/nginx.conf)。在合适的位置添加以下配置项: ``` location / { auth_request /auth; ... } location = /auth { internal; proxy_pass http://auth_backend; ... } ``` 这里的 "/auth" 是用来处理认证请求的URL,你可以根据需要进行修改。"http://auth_backend" 是实际处理认证请求的后端服务地址,也需要根据实际情况进行修改。 8. 保存并关闭配置文件后,重启Nginx服务: ``` sudo service nginx restart ``` 现在,你已经成功地添加了http_auth_request_module模块Nginx中。你可以根据实际需求进一步配置和使用该模块来进行认证授权操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

姜太小白

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值