×

nginx 查询参数的匹配

2021-07-17 20:36:42 Falcon

 

server {
  #... common definitions such as server, root

  location / {
    error_page 418 = @queryone;
    error_page 419 = @querytwo;
    error_page 420 = @querythree;

    if ( $query_string = "service=git-receive-pack" ) { return 418; }
    if ( $args ~ "service=git-upload-pack" ) { return 419; }
    if ( $arg_somerandomfield = "somerandomvaluetomatch" ) { return 420; }

    # do the remaining stuff
    # ex: try_files $uri =404;

  }

  location @queryone {
    # do stuff when queryone matches
  }

  location @querytwo {
    # do stuff when querytwo matches
  }

  location @querythree {
    # do stuff when querythree matches
  }
}
 

可以使用$query_string$args$arg_fieldname。支持正则匹配和精确匹配,这里使用了error_page和非标准的HTTP响应码,可以在官方文档中了解有关error_page的更多信息。

警告:请确保不要使用标准的HTTP响应码

本文收录于