2017-08-02 53 views
0

我正在使用openresty nginx v1.11.2.4。我希望能够在用户访问资源之前或者在他们尝试在服务器上放置某些内容之前对其进行身份验证。我使用的http_auth_request_module和下面的是,除了从我的nginx.conf文件:如何处理来自www-authenticate在nginx中的响应?

location /video/ { 
     auth_request /auth; 
     root /usr/local/openresty/nginx/html; 
    } 

    location = /auth { 
     more_set_headers "WWW-Authenticate: Basic"; 
     return 401; 
    } 

这将导致浏览器要求用户凭据正常的,但现在我如何才能/处理来自客户端的用户凭据?

回答

-1

的答案,这里的问题如下:Nginx authentication with auth_request module 我能够在我的nginx.conf文件访问$ HTTP_AUTHORIZATION变量来处理用户名和密码。以下是我的nginx.conf的摘录:

location /video { 
     satisfy any; 
     auth_basic "Private Property"; 
     auth_basic_user_file /usr/local/openresty/nginx/conf/htpasswd; 
     auth_request /auth; 
     root /usr/local/openresty/nginx/html; 
     client_max_body_size 1000M; 
     if ($request_method != "GET"){ 
      content_by_lua_file /root/Documents/contentbylua.lua; 
     } 
    } 

location = /auth { 
    set $authHeader $http_authorization; 
    set $authUName $remote_user; 
    content_by_lua_file /root/Documents/restrict.lua; 
} 

下的conf让我其证书存储在redisDB在200或401码取决于其返回restrict.lua文件中的用户进行身份验证用户凭据返回到/位置块。

响应(用户名&密码)由ngx.var.authHeader在restrict.lua文件中访问。一些字符串处理是为了移除'Basic',然后剩余部分被base64解码,然后对其进行一些字符串处理以获得密码。这就是全部

+0

这实际上是误用了ngx_http_auth_request_module和ngx_http_auth_basic_module。如果您基于Redis实施auth,只需在同一位置的access_by_lua_ *内运行您的代码即可。从Lua中你已经可以访问所有的nginx变量。无需设置指令。为什么你使用auth_basic *指令? –

+0

我使用auth_basic指令来提示输入用户凭证。我看到你的不必要的设置指令。 – Ayudh

+0

将access_by_lua *添加到/视频位置。检查授权标头是否存在。如果没有 - 用401和WWW-Authenticate进行响应。如果授权存在,请根据Redis进行检查。并完全删除ngx_http_auth_request_module和ngx_http_auth_basic_module。 –