且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

nginx的高级配置(1)——为某个虚拟主机添加用户验证

更新时间:2022-10-02 18:00:21

版本:centos6.5 ; mysql-5.6.26 ;nginx-1.6.3 ;php-5.4.37

环境架构:LNMP

1.配置第二个虚拟主机

可以在nginx.conf 加一行

include  vhosts/*.conf;

这样,我们就可以在 /usr/local/nginx/conf/vhosts目录下创建虚拟主机配置文件了。 

#mkdir /usr/local/nginx/conf/vhosts

#cd !$

#vim  111.conf   // 加入

server


server

{

    listen 80;

    server_name 192.168.137.100;

    index index.html index.htm index.php;

    root /data/www;

    location ~ .*admin\.php$ {

        auth_basic "xiayun Auth";

        auth_basic_user_file /usr/local/nginx/conf/htpasswd;

        include fastcgi_params;

        fastcgi_pass unix:/tmp/www.sock;

        fastcgi_index index.php;

        fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;


    }

    location ~ \.php$ {

        include fastcgi_params;

        fastcgi_pass unix:/tmp/www.sock;

        fastcgi_index index.php;

        fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;

    }


}

2.用户认证

首先需要安装apache,可以使用yum install httpd 安装

生成密码文件,创建用户

htpasswd -c /usr/local/nginx/conf/htpasswd  test 

添加test用户,第一次添加时需要加-c参数,第二次添加时不需要-c参数

总结:用户认证可以使用户登录某个路径时进行用户认证,提高保密度







      本文转自YU文武貝 51CTO博客,原文链接:http://blog.51cto.com/linuxerxy/1718238,如需转载请自行联系原作者