Skip to content

#1,屏蔽method

  • 在server 、location 中
if ($request_method !~ ^(GET|HEAD)$ ) {
            return    444;
}
if ($request_method !~ ^(GET|HEAD)$ ) {
            return    444;
}

2,if中的几种判断条件

正则表达式匹配:

  • == 等值比较;
  • ~ 与指定正则表达式模式匹配时返回“真”,判断匹配与否时区分字符大小写;
  • ~* 与指定正则表达式模式匹配时返回“真”,判断匹配与否时不区分字符大小写;
  • !~ 与指定正则表达式模式不匹配时返回“真”,判断匹配与否时区分字符大小写;
  • !~* 与指定正则表达式模式不匹配时返回“真”,判断匹配与否时不区分字符大小写;
if语法
if (表达式) {
    ...
}


当变量的值为空字符串或“ 0”时,变量为false ;在1.0.1版之前,任何以“ 0” 开头的字符串都被视为false值。

使用“ =”和“ !=”运算符比较变量和字符串;

变量使用“ ~”(对于区分大小写的匹配)和“ ~*”(对于不区分大小写的匹配)运算符与正则表达式进行匹配。正则表达式可以包含可供以后在$1.. $9变量中重用的捕获。也可以使用!取反。

使用“ -f”和“ !-f”运算符检查文件是否存在;

使用“ -d”和“ !-d”运算符检查目录是否存在;

使用“ -e”和“ !-e”运算符检查文件,目录或符号链接是否存在;

使用“ -x”和“ !-x”运算符检查可执行文件。

#为了配置if的条件判断,这里需要用到nginx中内置的全局变量
$args               这个变量等于请求行中的参数,同$query_string
$content_length     请求头中的Content-length字段。
$content_type       请求头中的Content-Type字段。
$document_root      当前请求在root指令中指定的值。
$host               请求主机头字段,否则为服务器名称。
$http_user_agent    客户端agent信息
$http_cookie        客户端cookie信息
$limit_rate         这个变量可以限制连接速率。
$request_method     客户端请求的动作,通常为GET或POST。
$remote_addr        客户端的IP地址。
$remote_port        客户端的端口。
$remote_user        已经经过Auth Basic Module验证的用户名。
$request_filename   当前请求的文件路径,由root或alias指令与URI请求生成。
$scheme             HTTP方法(如http,https)。
$server_protocol    请求使用的协议,通常是HTTP/1.0或HTTP/1.1。
$server_addr        服务器地址,在完成一次系统调用后可以确定这个值。
$server_name        服务器名称。
$server_port        请求到达服务器的端口号。
$request_uri        包含请求参数的原始URI,不包含主机名,如:”/foo/bar.php?arg=baz”。
$uri                不带请求参数的当前URI,$uri不包含主机名,如”/foo/bar.html”。
$document_uri       与$uri相同
if语法
if (表达式) {
    ...
}


当变量的值为空字符串或“ 0”时,变量为false ;在1.0.1版之前,任何以“ 0” 开头的字符串都被视为false值。

使用“ =”和“ !=”运算符比较变量和字符串;

变量使用“ ~”(对于区分大小写的匹配)和“ ~*”(对于不区分大小写的匹配)运算符与正则表达式进行匹配。正则表达式可以包含可供以后在$1.. $9变量中重用的捕获。也可以使用!取反。

使用“ -f”和“ !-f”运算符检查文件是否存在;

使用“ -d”和“ !-d”运算符检查目录是否存在;

使用“ -e”和“ !-e”运算符检查文件,目录或符号链接是否存在;

使用“ -x”和“ !-x”运算符检查可执行文件。

#为了配置if的条件判断,这里需要用到nginx中内置的全局变量
$args               这个变量等于请求行中的参数,同$query_string
$content_length     请求头中的Content-length字段。
$content_type       请求头中的Content-Type字段。
$document_root      当前请求在root指令中指定的值。
$host               请求主机头字段,否则为服务器名称。
$http_user_agent    客户端agent信息
$http_cookie        客户端cookie信息
$limit_rate         这个变量可以限制连接速率。
$request_method     客户端请求的动作,通常为GET或POST。
$remote_addr        客户端的IP地址。
$remote_port        客户端的端口。
$remote_user        已经经过Auth Basic Module验证的用户名。
$request_filename   当前请求的文件路径,由root或alias指令与URI请求生成。
$scheme             HTTP方法(如http,https)。
$server_protocol    请求使用的协议,通常是HTTP/1.0或HTTP/1.1。
$server_addr        服务器地址,在完成一次系统调用后可以确定这个值。
$server_name        服务器名称。
$server_port        请求到达服务器的端口号。
$request_uri        包含请求参数的原始URI,不包含主机名,如:”/foo/bar.php?arg=baz”。
$uri                不带请求参数的当前URI,$uri不包含主机名,如”/foo/bar.html”。
$document_uri       与$uri相同

3、if实现多重判断

1种

Nginx.conf中加入在你项目的正确位置

    set $my_ip ''; 
    if ( $remote_addr = 222.222.222.222){set $my_ip 1;} #注意这里的$remote_addr如何用了负载均衡的话,这里应该是$http_x_forwarded_for
    if ( $remote_addr = 192.168.1.170 ){ set $my_ip 1;}
    if ( $remote_addr = 192.168.1.169 ){ set $my_ip 1;}
    if ( $my_ip != 1) {rewrite ^/design/(.*)\.php$ /tingfu.html?$1&;}  #将*.php转到tingfu.html

2、访问某个php应用的时候我只想让内部的某个IP访问,其他的IP都转到另一个PHP上。如下:
访问test.php,且IP不等222.222.222.222的跳转到55555.php

set $test '';
if ( $request_uri ~* /img/test.php ) {
        set $test P;
}


if ( $http_x_forwarded_for !~* ^222\.222\.222\.222.* ) {
        set $test "${test}C";
}


if ( $test = PC ) {  #当条件符合 访问test.php并且 ip不是222.222.222.222的 转发到55555.php
rewrite ^(.*)$ /img/55555.php permanent;  
}
Nginx.conf中加入在你项目的正确位置

    set $my_ip ''; 
    if ( $remote_addr = 222.222.222.222){set $my_ip 1;} #注意这里的$remote_addr如何用了负载均衡的话,这里应该是$http_x_forwarded_for
    if ( $remote_addr = 192.168.1.170 ){ set $my_ip 1;}
    if ( $remote_addr = 192.168.1.169 ){ set $my_ip 1;}
    if ( $my_ip != 1) {rewrite ^/design/(.*)\.php$ /tingfu.html?$1&;}  #将*.php转到tingfu.html

2、访问某个php应用的时候我只想让内部的某个IP访问,其他的IP都转到另一个PHP上。如下:
访问test.php,且IP不等222.222.222.222的跳转到55555.php

set $test '';
if ( $request_uri ~* /img/test.php ) {
        set $test P;
}


if ( $http_x_forwarded_for !~* ^222\.222\.222\.222.* ) {
        set $test "${test}C";
}


if ( $test = PC ) {  #当条件符合 访问test.php并且 ip不是222.222.222.222的 转发到55555.php
rewrite ^(.*)$ /img/55555.php permanent;  
}

2种

if ($remote_addr ~ "^(12.34|56.78)" && $http_user_agent ~* "spider") {
    return 403;
}


set $flag 0;
if ($remote_addr ~ "^(12.34|56.78)") {
    set $flag "${flag}1";
}
if ($http_user_agent ~* "spider") {
    set $flag "${flag}2";
}
if ($flag = "012") {
    return 403;
}


if ($request_uri ~ "xxx.php?xxx") {
	set $iftmp Y;
}
if ($http_user_agent ~ "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; SV1)") {
	set $iftmp "${iftmp}Y";
}
if ($iftmp = YY) { return 400;}
location / {
	proxy_pass https://172.0.0.11;
}
if ($remote_addr ~ "^(12.34|56.78)" && $http_user_agent ~* "spider") {
    return 403;
}


set $flag 0;
if ($remote_addr ~ "^(12.34|56.78)") {
    set $flag "${flag}1";
}
if ($http_user_agent ~* "spider") {
    set $flag "${flag}2";
}
if ($flag = "012") {
    return 403;
}


if ($request_uri ~ "xxx.php?xxx") {
	set $iftmp Y;
}
if ($http_user_agent ~ "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; SV1)") {
	set $iftmp "${iftmp}Y";
}
if ($iftmp = YY) { return 400;}
location / {
	proxy_pass https://172.0.0.11;
}

重点一 set $iftmp Y 将iftmp设置为Y

重点二 还是set部分set $iftmp "${iftmp}Y" 在set里面 ${iftmp}Y将参数叠加一次,${iftmp}是一个大大的亮点.

$request_uri是客户端请求的完整请求路径,$http_user_agent是用户端发出请求的浏览器参数. 上面的if判断是一个简单防cc攻击的原型,一般普通的cc攻击会固定攻击页面,然后浏览器参数也大多不变,当前两个if参数同时满足后第三个if就直接返回给请求端400错误

 if ($remote_addr ~ "/forum.php" && $query_string ~* "mod=ajax&action=downremoteimg&message=") {  
  return 403;
}
#等同于
 set $flag 0;
        if ($request_uri ~* "/forum.php"){
            set $flag "${flag}1";
        }
        if ($query_string ~* "mod=ajax&action=downremoteimg&message=" ){
            set $flag "${flag}2";
        }
        if ($flag = "012") {
            return 403;
        }
 if ($remote_addr ~ "/forum.php" && $query_string ~* "mod=ajax&action=downremoteimg&message=") {  
  return 403;
}
#等同于
 set $flag 0;
        if ($request_uri ~* "/forum.php"){
            set $flag "${flag}1";
        }
        if ($query_string ~* "mod=ajax&action=downremoteimg&message=" ){
            set $flag "${flag}2";
        }
        if ($flag = "012") {
            return 403;
        }

遇到不法商家伪造站点跳转真实页面怎么办?

if ( $http_referer  ~* "https://www.xxxx.com" )  
    {
        return 307 https://weidian.com/?userid=163279346;
    }
if ( $http_referer  ~* "https://www.xxxx.com" )  
    {
        return 307 https://weidian.com/?userid=163279346;
    }

ip访问

set $my_ip '';

# 注意这里的 $remote_addr 如何用了负载均衡的话,这里应该是 $http_x_forwarded_for
if ( $remote_addr = 222.222.222.222) {
    set $my_ip 1;
}
if ( $remote_addr = 192.168.1.170 ) { 
    set $my_ip 1;
}
if ( $remote_addr = 192.168.1.169 ) { 
    set $my_ip 1;
}

# 将 *.php 转到 tingfu.html
if ( $my_ip != 1) {
    rewrite ^/design/(.*)\.php$ /tingfu.html?$1&;
}
set $my_ip '';

# 注意这里的 $remote_addr 如何用了负载均衡的话,这里应该是 $http_x_forwarded_for
if ( $remote_addr = 222.222.222.222) {
    set $my_ip 1;
}
if ( $remote_addr = 192.168.1.170 ) { 
    set $my_ip 1;
}
if ( $remote_addr = 192.168.1.169 ) { 
    set $my_ip 1;
}

# 将 *.php 转到 tingfu.html
if ( $my_ip != 1) {
    rewrite ^/design/(.*)\.php$ /tingfu.html?$1&;
}

访问特定页面

访问test.php,且IP不等 222.222.222.222 的跳转到 55555.php

set $test '';

if ( $request_uri ~* /img/test.php ) {
    set $test P;
}

if ( $http_x_forwarded_for !~* ^222\.222\.222\.222.* ) {
    set $test "${test}C";
}

if ( $test = PC ) {  #当条件符合 访问 test.php 并且 ip 不是 222.222.222.222 的 转发到 55555.php
    rewrite ^(.*)$ /img/55555.php permanent; 
}
set $test '';

if ( $request_uri ~* /img/test.php ) {
    set $test P;
}

if ( $http_x_forwarded_for !~* ^222\.222\.222\.222.* ) {
    set $test "${test}C";
}

if ( $test = PC ) {  #当条件符合 访问 test.php 并且 ip 不是 222.222.222.222 的 转发到 55555.php
    rewrite ^(.*)$ /img/55555.php permanent; 
}

geoip2

if ($geoip2_data_country_code != CN) {
    return 444;
 }
if ($geoip2_data_country_name != China){
    return 444;
}
if ($geoip2_data_country_code != CN) {
    return 444;
 }
if ($geoip2_data_country_name != China){
    return 444;
}
http {
geoip_country /usr/share/GeoIP/GeoIP.dat;   #GeoIP所在目录
map $geoip_country_code $allowed_country {  #变量判断
default yes;   #允许
CN no;  #区域不允许,这个CN就是代表中国,如果是多个地区,就是CN下面加就行
}

}


geoip_country GeoIP/GeoIP.dat;
geoip_city GeoIP/GeoLiteCity.dat;




location / {
root /data/wwwroot/test;  #网站目录
if ($allowed_country = no) {  #这里的no,就是上面html里面CN on,就是判断no区域
 
#if ($allowed_country = no) 也可以用if ($geoip_country_code = CN) 来代替,如果是多个区域就在CN后面几个|然后加区域代码
return 403;   #返回403提示
return http://域名; #跳转到其他人网站去 ,return也可以用rewrite,具体看自己网站的配置文件怎么设置的
root /data/wwwroot/test1;  #跳转到自己服务器的另外一个文件夹下面去
}
}


################

http {
    # 只提取关键配置参数
     
    geoip2 /usr/local/GeoLite2-Country_20190507/GeoLite2-Country.mmdb {
        $geoip2_data_country_code default=CN country iso_code;
    }
 
    server {
        add_header "country" $geoip2_data_country_code; #添加个响应头,方便查。
         
        location / {
            if ($geoip2_data_country_code = CN) {
                root /mnt/cn;
            }
             
            if ($geoip2_data_country_code != CN) {
                root /mnt/other;
            }
        }
    }
}


##############

set $deny 0;
    if ($geoip_country_code != "CN"){
        set $deny 1;
        return 302 $scheme://$host/405.html;
    }
    if ($deny = 1){
        return 302 $scheme://$host/405.html;
    }
http {
geoip_country /usr/share/GeoIP/GeoIP.dat;   #GeoIP所在目录
map $geoip_country_code $allowed_country {  #变量判断
default yes;   #允许
CN no;  #区域不允许,这个CN就是代表中国,如果是多个地区,就是CN下面加就行
}

}


geoip_country GeoIP/GeoIP.dat;
geoip_city GeoIP/GeoLiteCity.dat;




location / {
root /data/wwwroot/test;  #网站目录
if ($allowed_country = no) {  #这里的no,就是上面html里面CN on,就是判断no区域
 
#if ($allowed_country = no) 也可以用if ($geoip_country_code = CN) 来代替,如果是多个区域就在CN后面几个|然后加区域代码
return 403;   #返回403提示
return http://域名; #跳转到其他人网站去 ,return也可以用rewrite,具体看自己网站的配置文件怎么设置的
root /data/wwwroot/test1;  #跳转到自己服务器的另外一个文件夹下面去
}
}


################

http {
    # 只提取关键配置参数
     
    geoip2 /usr/local/GeoLite2-Country_20190507/GeoLite2-Country.mmdb {
        $geoip2_data_country_code default=CN country iso_code;
    }
 
    server {
        add_header "country" $geoip2_data_country_code; #添加个响应头,方便查。
         
        location / {
            if ($geoip2_data_country_code = CN) {
                root /mnt/cn;
            }
             
            if ($geoip2_data_country_code != CN) {
                root /mnt/other;
            }
        }
    }
}


##############

set $deny 0;
    if ($geoip_country_code != "CN"){
        set $deny 1;
        return 302 $scheme://$host/405.html;
    }
    if ($deny = 1){
        return 302 $scheme://$host/405.html;
    }