Nginx模塊系列之auth_basic模塊

1.1 介紹

ngx_http_auth_basic_module模塊實現讓訪問着,只有輸入正確的用戶密碼才容許訪問web內容。
web上的一些內容不想被其餘人知道,可是又想讓部分人看到。nginx的http auth模塊以及Apache http auth都是很好的解決方案。
默認狀況下nginx已經安裝了ngx_http_auth_basic_module模塊,若是不須要這個模塊,能夠加上 --without-http_auth_basic_module 。
nginx basic auth指令
語法: auth_basic string | off;
默認值: auth_basic off;
配置段: http, server, location, limit_except
默認表示不開啓認證,後面若是跟上字符,這些字符會在彈窗中顯示。
語法: auth_basic_user_file file;
默認值: —
配置段: http, server, location, limit_excepphp

1.2 安裝服務htpasswd

若是服務器上面沒有htpasswd 能夠使用yum install -y httpd 進行安裝html

[root@linux-node1 ~]# yum install httpd -y

1.3 添加用戶生成密碼文件

方法一:node

[root@linux-node1 conf]# printf "test1:$(openssl passwd -crypt 123456)\n" >>conf/htpasswd
[root@linux-node1 conf]# pwd
/etc/nginx/conf
[root@linux-node1 conf]# ll
total 4
-rw-r--r-- 1 root root 43 Dec 28 12:07 htpasswd
[root@linux-node1 conf]# cat htpasswd 
syavingc:uu7RndQCc/s.Q
test1:myirmIogiWWeQ

方法二:linux

[root@linux-node1 ~]# htpasswd -c /etc/nginx/passwd.db syavingc ##syavingc 爲用戶名
New password: ##設置密碼
Re-type new password: ##再次輸入密碼
Adding password for user syavingc

1.4 修改nginx配置文件

[root@linux-node1 vhost]# cat abc.conf
server {
listen 80;
server_name abc.12345.com;
location / {
root html/abc;
index index.php index.html index.htm;
auth_basic "nginx basic http test for syaving.com";
auth_basic_user_file /etc/nginx/passwd.db;
autoindex on;
}
access_log logs/access_abc.log main gzip buffer=32k flush=5s;
}

備註:必定要注意auth_basic_user_file路徑,不然會不厭其煩的出現403。
生成密碼
能夠使用htpasswd,或者使用opensslnginx

1.5 檢測語法,重啓nginx

[root@linux-node1 vhost]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@linux-node1 vhost]# nginx -s reload

1.6 輸出網址 用戶名 密碼

用戶名 syavingc
密碼 1q2w3e$Rweb

1.7 登錄以後的頁面

1.8 添加一個用戶

能夠按照下面方式添加新用戶,也能夠使用上面的方法進行添加新用戶服務器

[root@linux-node1 nginx]# echo -n 'test2:' >> /etc/nginx/conf/htpasswd 

1.9 設置加密密碼

[root@linux-node1 nginx]# openssl passwd 111111 >>/etc/nginx/conf/htpasswd

1.10 查看

[root@linux-node1 nginx]# cat /etc/nginx/conf/htpasswd
syavingc:uu7RndQCc/s.Q
test1:myirmIogiWWeQ
test2:5q4cr0mSI5VWU
[root@linux-node1 nginx]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@linux-node1 nginx]# nginx -s reload
相關文章
相關標籤/搜索