美文网首页
ngx_http_auth_request_module 第三方

ngx_http_auth_request_module 第三方

作者: 佛心看世界 | 来源:发表于2019-05-04 11:23 被阅读0次

原理

# 编译 Nginx 时需要添加该模块 --with-http_auth_request_module
# 该模块可以将客户端输入的用户名、密码 username:password 通过 Base64 编码后写入 Request Headers 中
# 例如:wang:wang -> Authorization:Basic d2FuZzp3YW5n=
# 然后通过第三方程序解码后跟数据库中用户名、密码进行比较,Nginx 服务器通过 header 的返回状态判断是否认证通过。


本地服务器配置

# 我们先来编辑本机配置文件,也就是用户直接访问的域名
shell > vim /usr/local/nginx-1.10.2/conf/vhost/local.conf  


server {
    listen 80;
    server_name local.server.com;

    auth_request /auth;

    location / {
        root   html;
        index  index.html;
    }

    location /auth {
        proxy_pass http://auth.server.com/HttpBasicAuthenticate.php;
        proxy_pass_request_body off;
        proxy_set_header Content-Length "";
        proxy_set_header X-Original-URI $request_uri;
    }
}

验证服务器配置

shell > vim /usr/local/nginx-1.10.2/conf/vhost/auth.conf  # 这是第三方认证服务器,认证逻辑使用的 PHP 代码

server {
    listen       80;
    server_name  auth.server.com;

    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  /usr/local/nginx-1.10.2/html$fastcgi_script_name;
        include        fastcgi_params;
    }
}

验证程序


<?php

if(isset($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW'])){
    $username = $_SERVER['PHP_AUTH_USER'];
    $password = $_SERVER['PHP_AUTH_PW'];

    if ($username == 'wang' && $password == '123456'){
        return true;
    }
}

header('WWW-Authenticate: Basic realm="Git Server"');
header('HTTP/1.0 401 Unauthorized');

相关文章

网友评论

      本文标题:ngx_http_auth_request_module 第三方

      本文链接:https://www.haomeiwen.com/subject/wxlxoqtx.html