1. 程式人生 > >Nginx 訪問認證

Nginx 訪問認證

span oot list 賬號密碼 工具 dmi index abc logs

有些網站,要求使用賬號和密碼才能訪問,如網站後臺、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來產生賬號和密碼,所以要先安裝http
htpasswd -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

技術分享

Nginx 訪問認證