nginx訪問認證+目目錄瀏覽

概述

在實際工作中,企業中有些網站,要求使用賬號和密碼才能訪問,如網站後台、phpMyAdmin 、Wiki 平台 等 模塊ngx_http_auth_basic_module 允許使用「HTTP基本認證」協議驗證用戶名和密碼來限制對資源的訪問 模塊ngx_http_auth_basic_module 下有兩條指令 auth_basic 和 auth_basic_user_file

環境

Centos 6.9 

安裝epel

wget https://mirrors.aliyun.com/epel/epel-release-latest-6.noarch.rpm  rpm -ivh epel-release-latest-6.noarch.rpm

安裝nginx

yum intall -y nginx

創建密鑰文件

yum install -y httpd-tools  htpasswd -bc /etc/nginx/conf.d/htpasswd.users username password

注意:username和password,分別對應用名和密碼

配置nginx

創建新的配置

vi /etc/nginx/conf.d/browse.conf

內容如下:

server {          listen       81;          server_name  localhost;          location / {                  root   /data/log/tomcat;                  index  index.html index.htm;                  # 設置用於認證的提示字符串                  auth_basic "Restricted Access";                  # 設置認證的密碼文件                  auth_basic_user_file  /etc/nginx/conf.d/htpasswd.users;                  #自動顯示目錄                  autoindex  on;                  #改為off後,顯示出文件的大概大小,單位是kB或者MB或者GB;即人性化方式顯示文件大小否則以byte顯示                  autoindex_exact_size  off;                   autoindex_localtime on;          }  }

重載配置

nginx -s reload

訪問頁面

輸入用戶名和密碼

效果如下: