nginx 用户认证
作用:web上的一些内容不想被其他人知道,但是又想让部分人看到。nginx的http auth模块以及Apache http auth都是很好的解决方案。默认情况下nginx已经安装了ngx_http_auth_basic_module模块,如果不需要这个模块,可以加上 --without-http_auth_basic_module 。
配置:
修改nginx.conf文件
server {
[*] listen 80;
[*] server_namelocalhost;
[*] auth_basic "Input Password:"; //认证提示符
[*] auth_basic_user_file "/usr/local/nginx/pass"; //认证密码文件
[*] location / {
[*] root html;
[*] indexindex.html index.htm;
[*] }
[*] }
生成密码文件,创建用户及密码
使用htpasswd命令创建账户文件,需要确保系统中已经安装了httpd-tools。
[*] # yum -y installhttpd-tools
[*] # htpasswd -cm /usr/local/nginx/pass tom //创建密码文件,注意pass的位置
[*] New password:
[*] Re-type new password:
[*] Adding password for user tom
[*] # htpasswd -m /usr/local/nginx/pass jerry
[*] //追加用户,不使用-c选项
[*] New password:
[*] Re-type new password:
[*] Adding password for user jerry
重启Nginx服务
页:
[1]