nginx配置站点密码访问办法
阿里云上面放了个nginx,但是不希望别人访问,除了吧url搞得非常长并且奇怪(这样只有你告诉别人才知道地址。。。)
环境:Ubuntu
linux下都差不多,可能某些配置文件已经命令有微小区别;
但是这样还是不够严谨,所以还是配置下账号密码。
nginx配置文件中加入
location / {
root html;
index index.html index.htm index index.jpg;
auth_basic 'Restricted';
auth_basic_user_file htpasswd;
}
密码文件不能简单的在文件中写入user:password
因为nginx 的密码必须是经过什么加密的。网上有用一些什么脚本的,不甚了解,还是使用htpasswd这个工具生成吧。
htpasswd -c /usr/local/nginx/conf/htpasswd test
test 就是你的用户名,
/usr/local/nginx/conf/htpasswd 是密码文本存放的路径(这跟你的nginx.conf 配置文件中有关
但是运行发现报错
The program 'htpasswd' is currently not installed. You can install it by typing:
apt-get install apache2-utils
这个其实是apache 的一个工具,我本机并没有安装,所以需要安装下
sudo apt-get install apache2-utils
又报错了··
Err http://mirrors.aliyun.com/ubuntu/ trusty-proposed/main apache2-utils amd64 2.4.7-1ubuntu4.14
404 Not Found [IP: 115.28.122.210 80]
没事,不要怕apt-get update
之后就一路顺风了。
记得重启nginx 服务才能生效
还没有评论,来说两句吧...