在一些業務不能夠直接提供給外部人員查看,只能指定某些人來查看。爲了安全起見,限定訪問ip,外加在頁面上做個簡單的登入頁面認證。
操作很簡單,在這裏做個簡單的記錄,以便後續查看回憶。
操作系統CentOS 7.2
nignx 1.10.1
首先我們用Nginx提供HTTP的Basic Auth功能,配置了需要輸入的用戶名和密碼,才能訪問網站。
我們使用htpasswd來生成密碼信息,就先要安裝httpd-tools,因在httpd-tools中包含了htpasswd命令
我們要用在httpd-tools中htpasswd命令,來設置帳號密碼
一般安裝了httpd都會有。若沒有就yum install -y httpd-tools安裝一下
查看本地服務器是否安裝了httpd-tools,
$ rpm -qa | grep httpd-tools httpd-tools-2.4.6-45.el7.centos.4.x86_64
接下來就創建帳號密碼
$ htpasswd -c /data0/work/nginx/passwd.db test New password: Re-type new password: Adding password for user test
查看是否創建成功
$ cat /data0/work/nginx/passwd.db test:$apr1$QroBUTZr$UNtXwv5nS3/jtvTCIw96h/
發現帳號已存在,且爲加密非明文密碼
那我們就在nginx配置檔裏進行添加設定了
$ vim /data0/work/nginx/conf/nginx.conf server { listen 80 ; server_name test.xxx.com; charset utf-8; auth_basic "secrect"; ##加 auth_basic_user_file /data0/work/nginx/passwd.db; ##加 location / { root /data0/work/nginx/html/; index index.html; ##限定可訪問網站的ip allow 39.28.0.0/16; allow 47.29.0.0/16; allow 120.83.0.0/16; allow 202.6.0.0/16; deny all; } }
檢驗nginx配置是否有誤
$ /data0/work/nginx/sbin/nginx -t nginx: the configuration file /data0/work/nginx/conf/nginx.conf syntax is ok nginx: configuration file /data0/work/nginx/conf/nginx.conf test is successful
如果沒有問題的話,重新加載nginx服務
$ /data0/work/nginx/sbin/nginx -s reload
訪問網站頁面,會跳出身份驗證
用錯誤帳號或密碼訪問網站,會一直進不去,取消登入後,會跳出下列畫面
非指定訪問ip來訪問網站,直接出現403拒絕訪問
從以上結果上來看,設定符合要求,設置成功!