[root@LHQ www]# cd /usr/local/nginx/conf/vhosts/

[root@LHQ vhosts]# ls

default.conf  test.conf

[root@LHQ vhosts]# vim test.conf

将配置文件改为:

server

{

listen 80;

server_name www.test.com;

index index.html index.htm index.php;

root /data/www;

location ~ .*admin\.php$ {

       auth_basic "aminglinux auth";

       auth_basic_usre_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_pass 127.0.0.1:9000;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME /data/www$fastcgi_script_name;

}

}

需要创建 /usr/local/nginx/conf/.htpasswd文件,Nginx没有这样的工具,需要用Apache的那个生成密码的工具htpasswd.

有两种方法:

(1)使用yum安装

     #yum install httpd

(2)之前手动安装编译过Apache

[root@LHQ vhosts]# ls /usr/local/apache2/bin/htpasswd

/usr/local/apache2/bin/htpasswd

使用这个工具创建密码文件

[root@LHQ vhosts]# htpasswd -c /usr/local/nginx/conf/.htpasswd qiangzi (第一次创建需要加上-c,后面就不需要了;qiangzi是新创建的用户)

New password:            (输入新密码)

Re-type new password:        (重新输入新密码)

Adding password for user qiangzi

查看文件:

[root@LHQ vhosts]# cat /usr/local/nginx/conf/.htpasswd

qiangzi:yYSTWnXZmvyCE(冒号后面是加过密的密码)

检查文件是否正确:

[root@LHQ vhosts]# /usr/local/nginx/sbin/nginx -t

如果没错,则执行:

[root@LHQ vhosts]# /etc/init.d/nginx reload

重新载入 Nginx:                                           [确定]

用curl测试PHP解析:

[root@LHQ vhosts]# curl -x127.0.0.1:80 -uqiangzi:123456 www.test.com/admin.php