有些网站,要求使用账号和密码才能访问,如网站后台、phpMyAdmin 、Wiki 平台 等
server { listen 80; server_name blog.abc.com; location / { root html/blog; index index.html index.htm; auth_basic "xxxxxxxxxx"; # 设置用于认证的提示字符串 auth_basic_user_file /usr/local/nginx/conf/htpasswd; # 设置认证的密码文件 } }
yum install -y httpd # 要用到http的工具htpasswd来产生账号和密码,所以要先安装httphtpasswd -bc /usr/local/nginx/conf/htpasswd abc 1234 # 设置认证的账号密码,会保存在密码文件中,第一次使用要用-bc参数htpasswd -b /usr/local/nginx/conf/htpasswd def 5678 # 以后使用无需加-c参数chmod 400 /usr/local/nginx/conf/htpasswd chown nginx /usr/local/nginx/conf/htpasswd /usr/local/nginx/sbin/nginx -t /usr/local/nginx/sbin/nginx -s reload