sudo apt-get install apache2-utils
sudo htpasswd -c /etc/nginx/.htpasswd username
输入密码,提示如下:
New password:
Re-type new password:
Adding password for user exampleuser
htpaswd的.htpasswd文件格式如下:
login:password
注意:htpasswd需要对nginx运行用户可访问
添加配置到NGINX配置
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/.htpasswd;
配置示例文件
server {
listen 80;
server_name localhost;
charset utf-8;
location /static {
autoindex on;
alias 静态文件路径;
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/.htpasswd;
}
}
重启Nginx服务
sudo service nginx restart
本文详细介绍如何使用Nginx配置基本HTTP认证,包括安装Apache工具包、创建.htpasswd文件、配置Nginx以实现访问限制,以及重启Nginx服务的完整步骤。
1425

被折叠的 条评论
为什么被折叠?



