小知识:nginx配置文件location使用实例:屏蔽IP/屏蔽蜘蛛/防盗链/重写/重定向等

在上文《nginx.conf location 修饰符解释及示例详解》中,我们对nginx location有了一定的了解,在本文中,我们将继续通过多个实例来了解location指令。

参数解释

location [=|~|~*|^~]/uri/{} = 开头表示精确匹配^~ 开头表示uri以某个常规字符串开头,理解为匹配 url路径即可。nginx不对url做编码,因此请求为/static/20%/aa,可以被规则^~ /static/ /aa匹配到(注意是空格)。 ~ 开头表示区分大小写的正则匹配。 ~* 开头表示不区分大小写的正则匹配。 !~!~*分别为区分大小写不匹配及不区分大小写不匹配 的正则。 / 通用匹配,任何请求都会匹配到。

location使用实例

1、普通重写

location /{ if(!-e $request_filename){         rewrite  ^(.*)$  /index.php?s=$1  last; break; } }
新窗显示代码
复制代码

2、301重定向

server_name xxx.com www.xxx.com; if($host ~* xxx.com){     rewrite ^/(.*)$ http://www.xxx.com/$1 permanent; }
新窗显示代码
复制代码

把所有不带www的域名301永久重定向到带www的域名。

3、http跳转https

普通

rewrite ^(.*) https://www.xxx.com$1 permanent; 

有cdn

if( $http_from_https !=on){      rewrite ^(.*) https://www.xxx.com$1 permanent;       }
新窗显示代码
复制代码

4、取消目录执行权限

location ~*^/(uploads|templets|data)/.*.(php|php5){     deny  all; }
新窗显示代码
复制代码

5、屏蔽来源域名

location /{     valid_referers www.baidu.com www.360.cn; if($invalid_referer){ return403; } }
新窗显示代码
复制代码

6、防盗链

location ~* \.(gif|jpg|png|webp){    valid_referers none blocked domain.com *.domain.com server_names ~\.google\. ~\.baidu\.; if($invalid_referer){ return403; #rewrite ^/ http://www.domain.com/403.jpg; }    root /opt/www/image; }
新窗显示代码
复制代码

7、屏蔽IP地址

allow 1.1.1.2; allow all; deny all; deny 1.1.1.2 location ^~/xxx/xxx/xx/ {       allow 172.0.0.1;       allow xxx.xxx.0.0/8;#表示允许xxx.xxx.0.1~ xxx.xxx.255.254       allow xxx.0.0.0/16;#表示允许xxx.0.0.1~ xxx.255.255.254       allow xxx.xxx.xxx.x;       deny all; }
新窗显示代码
复制代码

前端还有cdn情况

map $http_x_forwarded_for  $clientIp { “”      $remote_addr; ~^(?P<firstAddr>[09\.]+),?.*$  $firstAddr; } if($clientIp ~*“127.0.0.1|127.0.0.2”){ return403; break; }
新窗显示代码
复制代码

8、屏蔽蜘蛛

if($http_user_agent ~“FeedDemon|JikeSpider|MJ12bot|heritrix|EasouSpider|LinkpadBot|Ezooms”) { return403; }
新窗显示代码
复制代码

9、禁止非GET|HEAD|POST方式的抓取

if($request_method !~^(GET|HEAD|POST)$){ return403; }
新窗显示代码
复制代码

语法总结

if语句

#判断访问域名: if ($host ~* test.com) #判断user_agent: if ($http_user_agent ~* “baiduspider” ) #判断访问来源域名: valid_referers www.baidu.com;if ($invalid_referer){return 403;} #判断METHOD: if ($request_method !~ ^(GET|HEAD|POST)$) #判断url中?后参数: if ($request_uri ~* ^/list.php\?([^_]+)(_[0-9]+)$) #判断url路径地址: if ($uri ~* ^/list.php\?([^_]+)(_[0-9]+)$) #判断ip: if ($remote_addr ~* “127.0.0.1|127.0.0.2”)

#判断访问域名: if ($host ~* test.com) #判断user_agent:  if ($http_user_agent ~* “baiduspider” ) #判断访问来源域名:  valid_referers www.baidu.com;if ($invalid_referer){return 403;} #判断METHOD:  if ($request_method !~ ^(GET|HEAD|POST)$) #判断url中?后参数: if ($request_uri ~* ^/list.php\?([^_]+)(_[0-9]+)$) #判断url路径地址:  if ($uri ~* ^/list.php\?([^_]+)(_[0-9]+)$) #判断ip:  if ($remote_addr ~* “127.0.0.1|127.0.0.2”)

新窗显示代码
复制代码

处理方式

#禁止访问: return 403; deny all; #重定向到: rewrite ^/(.*)$ http://www.test.com/$1 permanent;  #重写到: rewrite  ^(.*)$  /index.php?s=$1  last; 

#禁止访问: return 403; deny all; #重定向到: rewrite ^/(.*)$ http://www.test.com/$1 permanent;  #重写到: rewrite  ^(.*)$  /index.php?s=$1  last; 

新窗显示代码
复制代码

全局变量

$args $content_length $content_type $document_root $document_uri $host $http_user_agent $http_cookie $limit_rate $request_body_file $request_method $remote_addr $remote_port $remote_user $request_filename $request_uri $query

$args $content_length $content_type $document_root $document_uri $host $http_user_agent $http_cookie $limit_rate $request_body_file $request_method $remote_addr $remote_port $remote_user $request_filename $request_uri $query

新窗显示代码
复制代码

总结

本文通过多个实例介绍了nginx中的location指令的用法,你还可以阅读此文《nginx.conf location 修饰符解释及示例详解》了解更多有关nginx location的知识。

声明: 猿站网有关资源均来自网络搜集与网友提供,任何涉及商业盈利目的的均不得使用,否则产生的一切后果将由您自己承担! 本平台资源仅供个人学习交流、测试使用 所有内容请在下载后24小时内删除,制止非法恶意传播,不对任何下载或转载者造成的危害负任何法律责任!也请大家支持、购置正版! 。本站一律禁止以任何方式发布或转载任何违法的相关信息访客发现请向站长举报,会员发帖仅代表会员个人观点,并不代表本站赞同其观点和对其真实性负责。本网站的资源部分来源于网络,如有侵权烦请发送邮件至:2697268773@qq.com进行处理。
建站知识

小知识:如何在 Linux 中找到一个进程 ID 并杀死它

2023-3-14 3:01:39

建站知识

小知识:13个从头开始构建的独立 Linux 发行版

2023-3-14 3:10:06

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索