Nginx auto_index和auth_basic
1、nginx auto_index
nginx站點目錄瀏覽功能,默認情況下為關閉
啟用或禁用目錄列表輸出
開啟這個功能的前提是站點目錄下沒有首頁index.html
官方說明:http://nginx.org/en/docs/http/ngx_http_autoindex_module.html
語法Syntax: autoindex on | off;
默認值Default: autoindex off;
使用字段Context: http(全局選項), server(單個網站選項), location(針對單個location)
使用范圍:分享文件,搭建yum源等
1.1 在http下開啟
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
autoindex on;
server {
省略其他內容
1.2 在location下開啟
location / {
autoindex on;
省略其他內容
}
1.3 在server下開啟
server {
listen 80;
server_name localhost;
autoindex on;
location
省略其他內容
開啟目錄瀏覽功能后,即可通過瀏覽器訪問到站點並瀏覽目錄,也可針對於某一目錄
2、nginx auth_basic
有時,我們需要為網站設置訪問賬號和密碼權限,這樣操作后,只有擁有賬號密碼的用戶才能訪問網站內容,這種使用賬號密碼才可以訪問網站的功能主要應用在企業內部人員訪問的地址上,例如:企業網站后台,MySQL客戶端phpMyAdmin、企業內部的CRM、WIKI網站平台等。
官方說明:http://nginx.org/en/docs/http/ngx_http_auth_basic_module.html
2.1 HTTP基本認證協議啟用用戶名和密碼驗證
語法Syntax: auth_basic string(任意字符串) | off(關閉);
默認值Default: auth_basic off;
使用字段Context: http, server, location, limit_except
用法
修改配置文件,指定后面生成的密碼文件保存路徑(相對路徑或絕對路徑)
location / {
auth_basic "closed site";
auth_basic_user_file conf/htpasswd;
}
2.2 生成密碼文件的方法
2.2.1 通過軟件httpd-tools生成
安裝生成驗證密碼文件的軟件
yum install httpd-tools –y
查看安裝httpd-tools后可用的二進制命令
rpm -ql httpd-tools
[root@Web01 conf]# rpm -ql httpd-tools
/usr/bin/ab
/usr/bin/htdbm
/usr/bin/htdigest
/usr/bin/htpasswd
省略輸出
查看htpasswd用法
[root@Web01 conf]# htpasswd --help
Usage:
htpasswd [-cmdpsD] passwordfile username
htpasswd -b[cmdpsD] passwordfile username password
htpasswd -n[mdps] username
htpasswd -nb[mdps] username password
-c Create a new file.
-n Don't update file; display results on stdout.
-m Force MD5 encryption of the password.
-d Force CRYPT encryption of the password (default).
-p Do not encrypt the password (plaintext).
-s Force SHA encryption of the password.
-b Use the password from the command line rather than prompting for it.
-D Delete the specified user.
On Windows, NetWare and TPF systems the '-m' flag is used by default.
On all other systems, the '-p' flag will probably not work.
使用第二種方法非交互式一條命令生成密碼文件
[root@Web01 conf]# htpasswd -bc /application/nginx/conf/htpasswd test 123456
Adding password for user test
[root@Web01 conf]# cat /application/nginx/conf/htpasswd
test:Hf2ctuRKkWaig
2.2.2 在線生成
利用網站在線生成
鏈接: http://tool.oschina.net/htpasswd
輸入用戶名和密碼,選擇適合當前web服務器的加密算法,然后把生成的結果手動添加到密碼文件中
2.3 修改配置文件並生效
[root@Web01 conf]# vim nginx.conf
location / {
root html;
index index.html index.htm;
auth_basic "test";
auth_basic_user_file /application/nginx/conf/htpasswd;
}
省略其他內容
[root@Web01 conf]# nginx -t #檢查語法
nginx: the configuration file /application/nginx-1.12.2/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.12.2/conf/nginx.conf test is successful
[root@Web01 conf]# nginx -s reload #平滑重啟
清除瀏覽器緩存,訪問站點測試,輸入用戶名密碼后能進行訪問
博主原創文章,轉載請務必注明出處