對於一些需要簡單安全防護的場景可以使用Http Basic機制,比如:給Kibana、Elasticsearch添加簡單的登錄認證機制。
1、安裝apache2-utils or httpd-tools,以CentOS為例
<code>yum install httpd-tools/<code>
2、創建用戶
<code>htpasswd -c /etc/nginx/.htpasswd admin/<code>
按要求輸入兩遍密碼
New password:
Re-type new password:
.htpasswd文件創建後如果要追加用戶可以使用命令 "htpasswd /etc/nginx/.htpasswd 用戶名" 進行添加
3、打開nginx配置文件增加auth_basic和auth_basic_user_file,結果如下:
<code>server {
auth_basic "Administrator's Area";
\tauth_basic_user_file /etc/nginx/.htpasswd;
}/<code>
如果有些location不需要認證可以增加“auth_basic off;”關閉認證
location /public {
auth_basic off;
}
4、測試配置文件是否正確
<code>nginx -t/<code>
輸出以下結果說明配置無誤
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
5、reload,使配置生效
<code>nginx -s reload/<code>
6、用瀏覽器訪問就會要求輸入用戶名、密碼了
參考:Restricting Access with HTTP Basic Authentication
https://docs.nginx.com/nginx/admin-guide/security-controls/configuring-http-basic-authentication/
閱讀更多 chenjiafan 的文章