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

  • auth_request模块的用途

    【Nginx】基于http响应状态码做权限控制的auth_request模块_第1张图片

  • 安装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";
    }
    
    ​

你可能感兴趣的:(Nginx,nginx,http)