安装 htpasswd 工具
使用 apache2-utils``包来获得
htpasswd`工具:
1sudo apt-get install apache2-utils
创建 .htpasswd 文件
1sudo htpasswd -c /etc/nginx/.htpasswd yourusername
配置 Nginx 使用基本认证
在你的 Nginx 配置文件中,添加auth_basic
和auth_basic_user_file
指令到相应的location
块。例如:
1server {
2 listen 80;
3 server_name example.xyz;
4
5 # 第一个目录
6 location /downloads/ {
7 root /home/1; # 指向 /home/null/downloads
8 autoindex on;
9 autoindex_exact_size off;
10 autoindex_localtime on;
11
12 # 启用基本认证
13 auth_basic "Restricted Access";
14 auth_basic_user_file /etc/nginx/.htpasswd;
15 }
在上述配置中:
-
auth_basic "Restricted Access";
设置了一个认证提示文本。 -
auth_basic_user_file /etc/nginx/.htpasswd;
指定了 .htpasswd 文件的位置,Nginx 会根据这个文件中的用户和密码进行认证。