Ngnix由模块组成,今天做个记录
一、access访问控制模块
用法
允许allow
语法(Syntax):Syntax: allow address | CIDR | unix: | all;
使用位置(Context): http, server, location, limit_except
拒绝deny
语法(Syntax): deny address | CIDR | unix: | all;
使用位置(Context): http, server, location, limit_except
案例:只允许本机访问,其他拒绝,本次加在location中
location / {
root html;
index index.html index.htm;
allow 127.0.0.1;
deny all;
}
注:
1、也可以是IP段,如192.168.1.0/24
2、以小为原则 location > server >http
记得加载配置文件nginx -s reload
二、auth_basic_stub认证模块
用户认证模块,对访问目录进行加密,访问时需要用户密码认证,需要与认证文件匹配
用法:
Syntax: auth_basic string | off;
Default: auth_basic off; 默认不启用
Context: http, server, location, limit_except
Syntax: auth_basic_user_file file;
Default: —
Context: http, server, location, limit_except
案例:对网站开启认证功能,只语序列表中的帐号访问
1、生成认证文件:
安装软件支持
[root@localhost ~]# yum install httpd-tools -y
生成配置文件及密码:htpasswd工具,新增用户名yjy密码123456
[root@localhost ~]# htpasswd -c /usr/local/nginx/conf/auth_passwd.conf yjy
New password:
Re-type new password:
Adding password for user yjy
2、在配置文件中使用
location / {
root html;
index index.html index.htm;
auth_basic "user auth";
auth_basic_user_file /usr/local/nginx/conf/auth_passwd.conf;
}
效果
三、status状态模块
用来查看nginx服务状态
用法
Syntax: stub_status;
Default: —
Context: server, location
案例:只允许内网查看网站访问状态信息
location = /status {
stub_status;
allow 192.168.1.0/24;
deny all;
}
效果
四、referer防盗链模块
用途:防止第三方网站盗用本站图片等资源
用法
Syntax: valid_referers none | blocked | server_names | string …;
Default: —
Context: server, location
案例
location ~* \.(gif|jpg|png|swf|flv)$ {
valid_referers none blocked 192.168.1.111;
root /usr/local/nginx/html;
if ($invalid_referer) {
return 403;
}
}
注:
valid_referers:合法的引用
none: 在本网的操作
192.168.1.111:服务器的IP或域名,第一行可以理解为白名单的意思
效果:被盗用时,图片会呈现图裂的现象
-----------------end