一 通过htpasswd命令生成用户名及对应密码数据库文件
htpasswd -c /usr/local/nginx/passwd.db test
chmod 400 /usr/local/nginx/passwd.db
chown nginx. /usr/local/nginx/passwd.db
cat /usr/local/nginx/passwd.db
test:8eZAz7BqcrXmY
二:修改配置文件
vim nginx.conf
在原来的server下面新增一个虚拟主机,访问需要使用密码 www.it165.net
server
{
listen 980;
server_name it.net.cn;
index index.html index.htm index.php;
root /home/wwwroot;
location ~ .*\.(php|php5)?$
{
try_files $uri =404;
fastcgi_pass unix:/tmp/php-cgi.sock;
fastcgi_index index.php;
include fcgi.conf;
}
location /status {
stub_status on;
access_log off;
auth_basic "NginxStatus";
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 12h;
}
access_log /home/wwwlogs/access.log access;
}
include vhost/*.conf;
server {
listen 80; #//监听端口为8
server_name www.it.net.cn; #//虚拟主机网址
location / {
root /home/wwwroot/; # //虚拟主机网站根目录
index index.html index.htm; #//虚拟主机首页
auth_basic "secret";# //虚拟主机认证命名
auth_basic_user_file /usr/local/nginx/passwd.db;# //虚拟主机用户名密码认证数据库
}
location /status {
stub_status on; # //开启网站监控状态
access_log /usr/local/nginx/logs/www1_status.log;# //监控日志
auth_basic "NginxStatus"; }
}
}
三 做好本地的域名解析
xxx.xxx.xxx.xxx it.net.cn
四 访问测试