nginx 中配置HTTP Basic Authentication

    实际中开发中有没有想做个简单的管理页面?其网页内容就算暴露也没多大关系。虽然开源的管理后台很多,但是麻烦。最最简单的就是利用nginx的auth_basic。今天,有个爬虫小的模块要写,想想还是用nginx处理下,这样就能随时通过网页方便观察和调整了。

一、安装htpasswd

yum install httpd-tools -y

二、生成配置账号密码的文件

htpasswd -c /etc/nginx/conf.d/passwd/auth username
#username 为你要创建的账号

然后输入两次的密码即可

三、配置nginx文件

server {
        listen       80;
        server_name www.domain.com;
        index index.php index.htm index.html;
        #开始配置auth
        auth_basic "secret";
        #开始配置auth的文件地址
        auth_basic_user_file /etc/nginx/conf.d/passwd/auth;
        root  /usr/share/nginx/html/phpinfo;
        rewrite . /index.php;


        location ~ .*\.(php|php5)?$ {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME   $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
}

现在只要nginx -t 后 -s reload重启即可.效果如下图:


你可能感兴趣的:(nginx 中配置HTTP Basic Authentication)