nginx配置,模块(auth_basic/auth_basic/referer)

用户认证模块(auth_basic)

 

# 加密设置

        location /img {

                auth_basic "User Auth";   #设置名字

                auth_basic_user_file /usr/local/nginx/conf/auth.passwd; #用户认证文件放置路径

        }

 

# 生成认证文件

[[email protected] html]# yum install httpd-tools -y

[[email protected] html]# htpasswd -c /usr/local/nginx/conf/auth.passwd admin

New password:

Re-type new password:

Adding password for user admin

 

重启服务后,访问验证:

现在当我们访问img目录时,需要输入用户名及密码。

它不仅可以设置访问指定目录时认证,还可以直接在访问首页时认证。

如果需要关闭可以使用auth_basic offff;或者直接将这两段注释掉。

 

stub_status模块

设置访问控制,只允许本机查看nginx得status状态信息,其他人均拒绝;

 

nginx.conf配置设置

        location /status {

                stub_status;

                allow 192.168.68.137;

                deny all;

        }

本机访问

nginx配置,模块(auth_basic/auth_basic/referer)

192.168.68.136不可访问

nginx配置,模块(auth_basic/auth_basic/referer)

 

 

referer模块

设置防盗链,截图验证设置成功(出现图裂);

配置(192.168.68.137)

        location ~* \.(gif|jpg|png|swf|flv)$ {

                 valid_referers none blocked   *.jfedu.net;         #白名单

                 root /usr/local/nginx/html;

                 if ($invalid_referer) {

                          return 403;

                          }

                }

nginx配置,模块(auth_basic/auth_basic/referer)

 

图裂测试(192.168.68.136)

配置index.html

<html>

<h1>welcome to nginx</h1>

<img src="http://192.168.68.137/tupian.jpg">

</html>

nginx配置,模块(auth_basic/auth_basic/referer)