Nginx에 인증을 추가 할 사이트를 사용하는

Nginx에 인증을 추가 할 사이트를 사용하는

기본 인증

하지 모듈이 --without-http_auth_basic_module 추가 할 수있는 경우 기본 nginx를 이미 ngx_http_auth_basic_module 모듈이 설치됩니다.

nginx를 기본 인증 명령

구문 : auth_basic 문자열 | 오프,
기본값 : 오프 auth_basic,
구성 섹션 : HTTP, 서버, 위치, limit_except

그들은 다시는 문자로 유지하는 경우가 이러한 문자의 팝업에 나타납니다, 기본 인증을 열지 않았다.

구문 : auth_basic_user_file 파일,
기본 : -
구성 섹션 : HTTP, 서버, 위치, limit_except

사용자 암호 파일, 다음과 유사한 :

username:passwd:comment
用户名:密码:注释
  1. 비밀번호 암호화 도구로 htpasswd를 사용하여

    sudo apt install apache2-utils
    sudo htpasswd /usr/local/nginx/conf/htpasswd username //输入两遍密码
  2. 구성의 nginx 수정

    sudo vim /usr/local/nginx/conf/nginx.conf
    在http段添加:
        auth_basic "nginx basic http auth test for auth.test.com";
        auth_basic_user_file /usr/local/nginx/conf/htpasswd;
        autoindex_exact_size off;
        autoindex_localtime on;
        autoindex on;
  3. 다시 시작의 nginx 서비스

    sudo nginx -t
    sudo nginx -s reload
  4. 이제 그들은 로그인 전에 인증 할 필요가 도메인 이름으로 리소스에 대한 액세스 권한을 검증하기 위해 브라우저로 이동

ngx_http_auth_request_module 타사 인증

Nginx에 모듈을 컴파일하는 것은 --with-http_auth_request_module 추가해야
암호 서면 요청 헤더 Base64로가에 의해 인코딩 : 클라이언트 모듈의 사용자 이름 암호 사용자 이름을 입력 할 수 있습니다
> 인증 - 왕 : 예 : 왕 기본 d2FuZzp3YW5n을 =
다음 데이터베이스 사용자 이름을 디코딩하는 타사 프로그램, 암호를 비교 한 다음, Nginx의 서버는 헤더에 의해 인증 여부를 결정하기 위해 상태를 반환합니다.

  • 의 nginx 구성 파일을 편집
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;
    }
}

# auth_request /auth; # 启用认证
# proxy_pass http://auth.server.com/HttpBasicAuthenticate.php; # 认证服务器地址
# 参考地址:http://nginx.org/en/docs/http/ngx_http_auth_request_module.html
  • 타사 인증 서버 추가

    vim /usr/local/nginx/conf/vhost/auth.conf  # 这是第三方认证服务器,认证逻辑使用的 PHP 代码
    
    server {
        listen       80;
        server_name  auth.server.com;
        root /usr/local/nginx/auth;
    
        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;
        }
    }
  • 인증 프로그램 추가

    vim /usr/local/nginx/auth/HttpBasicAuthenticate.php
    
    <?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');
  • 사용자 액세스 local.server.com 팝업 상자의 사용자 이름을 입력하고 암호에 저장됩니다 $ _SERVER 변수
    사용자 이름 중간 부분 만 시연, 작업은 데이터베이스의 데이터와 비교를하기 위해 암호를 입력하도록해야하는 경우
    사용자 액세스 local.server.com는 액세스 local.server.com 인증 후 계속을 사용자 인증을 auth.servere.com 갈 것이다

추천

출처www.cnblogs.com/super-lulu/p/11741591.html