服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

云服务器|WEB服务器|FTP服务器|邮件服务器|虚拟主机|服务器安全|DNS服务器|服务器知识|Nginx|IIS|Tomcat|

服务器之家 - 服务器技术 - Nginx - nginx配置文件location使用实例:屏蔽IP/屏蔽蜘蛛/防盗链/重写/重定向等

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

2022-11-04 23:09网络 Nginx

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

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

参数解释


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

location使用实例

1、普通重写


  1. location / { 
  2.     if (!-e $request_filename) { 
  3.         rewrite  ^(.*)$  /index.php?s=$1  last; 
  4.         break;   
  5.     } 
  6. }

2、301重定向


  1. server_name xxx.com www.xxx.com; 
  2. if ($host ~* xxx.com) { 
  3.     rewrite ^/(.*)$ http://www.xxx.com/$1 permanent; 
  4. }

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

3、http跳转https

普通


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

有cdn


  1. if ( $http_from_https != 'on' ){
  2.      rewrite ^(.*) https://www.xxx.com$1 permanent;      
  3. }

4、取消目录执行权限


  1. location ~* ^/(uploads|templets|data)/.*.(php|php5){
  2.     deny  all;
  3. }

5、屏蔽来源域名


  1. location / {
  2.     valid_referers www.baidu.com www.360.cn;
  3.     if ($invalid_referer){
  4.         return 403;
  5.     }
  6. }

6、防盗链


  1. location ~* \.(gif|jpg|png|webp){
  2.    valid_referers none blocked domain.com *.domain.com server_names ~\.google\. ~\.baidu\.;
  3.    if ($invalid_referer) {
  4.     return 403;
  5.     #rewrite ^/ http://www.domain.com/403.jpg;
  6.    }
  7.    root /opt/www/image;
  8. }

7、屏蔽IP地址


  1. allow 1.1.1.2;
  2. allow all;
  3. deny all;
  4. deny 1.1.1.2
  5.  
  6. location ^~ /xxx/xxx/xx/
  7. {
  8.       allow 172.0.0.1;
  9.       allow xxx.xxx.0.0/8;#表示允许xxx.xxx.0.1 ~ xxx.xxx.255.254  
  10.       allow xxx.0.0.0/16;#表示允许xxx.0.0.1 ~ xxx.255.255.254
  11.       allow xxx.xxx.xxx.x; 
  12.       deny all;
  13. }

前端还有cdn情况


  1. map $http_x_forwarded_for  $clientIp {
  2.         ""      $remote_addr;
  3.         ~^(?P<firstAddr>[0-9\.]+),?.*$  $firstAddr;
  4. }
  5. if ($clientIp ~* "127.0.0.1|127.0.0.2") {
  6.    return 403;
  7.    break;
  8. }

8、屏蔽蜘蛛


  1. if ($http_user_agent ~ "FeedDemon|JikeSpider|MJ12bot|heritrix|EasouSpider|LinkpadBot|Ezooms" )
  2. {
  3.   return 403;
  4. }

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


  1. if ($request_method !~ ^(GET|HEAD|POST)$) {
  2.   return 403;
  3. }

语法总结

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")

处理方式

  • #禁止访问:
  • 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

总结

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

延伸 · 阅读

精彩推荐