溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Nginx web 網(wǎng)站訪問限制登入驗證

發(fā)布時間:2020-07-15 11:21:54 來源:網(wǎng)絡(luò) 閱讀:14261 作者:翹楚秦歌 欄目:建站服務(wù)器

在一些業(yè)務(wù)不能夠直接提供給外部人員查看,只能指定某些人來查看。為了安全起見,限定訪問ip,外加在頁面上做個簡單的登入頁面認(rèn)證。

操作很簡單,在這里做個簡單的記錄,以便后續(xù)查看回憶。


操作系統(tǒng)CentOS 7.2

nignx 1.10.1

首先我們用Nginx提供HTTP的Basic Auth功能,配置了需要輸入的用戶名和密碼,才能訪問網(wǎng)站。

我們使用htpasswd來生成密碼信息,就先要安裝httpd-tools,因在httpd-tools中包含了htpasswd命令

我們要用在httpd-tools中htpasswd命令,來設(shè)置帳號密碼

一般安裝了httpd都會有。若沒有就yum install -y httpd-tools安裝一下

查看本地服務(wù)器是否安裝了httpd-tools,

$ rpm -qa | grep httpd-tools
httpd-tools-2.4.6-45.el7.centos.4.x86_64


接下來就創(chuàng)建帳號密碼

$ htpasswd -c /data0/work/nginx/passwd.db test
New password: 
Re-type new password: 
Adding password for user test


查看是否創(chuàng)建成功 

$ cat  /data0/work/nginx/passwd.db
test:$apr1$QroBUTZr$UNtXwv5nS3/jtvTCIw96h/

發(fā)現(xiàn)帳號已存在,且為加密非明文密碼


那我們就在nginx配置檔里進(jìn)行添加設(shè)定了

$ 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;
        ##限定可訪問網(wǎng)站的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服務(wù)

$ /data0/work/nginx/sbin/nginx -s reload


訪問網(wǎng)站頁面,會跳出身份驗證

Nginx web 網(wǎng)站訪問限制登入驗證


用錯誤帳號或密碼訪問網(wǎng)站,會一直進(jìn)不去,取消登入后,會跳出下列畫面

Nginx web 網(wǎng)站訪問限制登入驗證


非指定訪問ip來訪問網(wǎng)站,直接出現(xiàn)403拒絕訪問

Nginx web 網(wǎng)站訪問限制登入驗證

從以上結(jié)果上來看,設(shè)定符合要求,設(shè)置成功!

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI