Nginx的location if 太奇怪了

需要请求的图片,其中$station_id是变量

 

http://host/thumb/200x/$station_id.jpg

http://host/thumb/160x/$station_id.jpg

 

为了容错,提供了两张默认图片,当$station_id对应的图片找不到的时候,用默认图片替代

http://host/thumb/notfound_160x.jpg

http://host/thumb/notfound_200x.jpg

 

 

为此在Nginx里面配置:

 

location ^~ /thumb/ {

       set $notfound "/thumb/notfound_160x.jpg";

       if ( $uri ~ "/200x/" )  {

          set $notfound "/thumb/notfound_200x.jpg";

       }

       try_files $uri $notfound =404;

       add_header Cache-Control max-age=100;

       log_not_found off;

       #access_log /usr/local/nginx/logs/a1.log access_format;

       access_log  off;

    }


但是当我们访问: http://host/thumb/160x/$station_id.jpg 的时候配置生效,返回小的默认图片; 访问 http://host/thumb/200x/$station_id.jpg 的时候,就一直提示NotFound


最后被迫无奈,不在loaction中if了,改用两个location

nginx中的location指令用于匹配请求的URL路径,并根据匹配结果进行处理。同时,可以使用if指令在location内部进行条件判断和分支处理。根据提供的引用内容,可以得到以下结论: 1. 在nginx中使用if指令进行条件判断时,需要注意if指令的使用方法和位置。引用中的示例代码展示了在location内部使用if指令进行条件判断的方法。 2. 在if指令的逻辑中,可以使用正则表达式进行匹配。引用中的示例代码展示了使用$query_string和$arg_uid变量进行参数匹配的例子。 3. 在多个if条件判断的情况下,需要注意判断条件的顺序和是否需要使用break指令。引用中的示例代码展示了根据条件匹配的优先级来设置if指令的顺序,并使用break指令来终止后续的条件判断。 综上所述,nginx中的locationif指令可以用于根据URL路径和参数进行条件判断和分支处理。需要注意if指令的使用方法、判断条件的顺序以及是否需要使用break指令来控制条件判断的终止。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [nginx location中多个if里面proxy_pass](https://blog.youkuaiyun.com/liuxiao723846/article/details/83147792)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值