Nginx(9)_if语句的使用

判断条件语法

语法 说明
$variable 仅为变量时,值为空或以0开头的字符串都会被当做false处理
= 或 != 相等或不相等比较
~ 或 !~ 正则匹配或非正则匹配
~* 正则匹配,不区分大小写
-f 或 !-f 检查文件存在或不存在
-d 或 !-d 检查目录存在或不存在
-e 或 !-e 检查文件、目录、符号链接等存在或不存在
-x 或 !-x 检查文件可执行或不可执行

示例:

server {
      listen       8080;
      server_name  localhost;
      charset      utf-8;
      location /images {
          if ($uri = "/images/") {
             rewrite /images/(.*) /download/$1;
          }
      }

     location /download/ {
         root /home/nginx;
      }
  }

注意:if后面要加空格,不然语法会报错。

发布了99 篇原创文章 · 获赞 29 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/u010982507/article/details/104031821