加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 运营中心 > Nginx > 正文

nginx – 如果作为参数传递的文件名没有退出,则重定向到404

发布时间:2020-12-13 21:12:49 所属栏目:Nginx 来源:网络整理
导读:我在uri中有特殊的参数data = / path / to / filename.htm. 在SSI上建立了旧的和生锈的网站,我无法修改它. 问题是当文件名传递为arg_data不存在时,页面被破坏. 在这种情况下,我想重定向到404.htm. 像这样的东西: if (!-f $arg_data){ rewrite ^.*$ /404.htm

我在uri中有特殊的参数data = / path / to / filename.htm.

在SSI上建立了旧的和生锈的网站,我无法修改它.

问题是当文件名传递为arg_data不存在时,页面被破坏.

在这种情况下,我想重定向到404.htm.

像这样的东西:

if (!-f $arg_data){
     rewrite ^.*$ /404.htm last;
   } 

但我得到:

rewrite or internal redirection cycle while processing “/404.htm

我认为是因为我没有检查arg_data是否存在.

但是nginx没有嵌套if-s.

我试过了:

set $data index.htm;                                                                                                                                               
if ($args ~ "data=(.+)") {                                                                                                                                         
    set $data $1;                                                                                                                                                  
}                                                                                                                                                                                                                                                                                                                                          
if (!-f $data) {                                                                                                                                                
    rewrite ^.*$ /404.htm last;                                                                                                                                   
}                   

Idea将$data设置为100%存在的文件,然后在重写之后传递数据参数.

由于某种原因,它给出了相同的错误,内部重定向循环

看起来我做错了.

最佳答案
好的,关于我们最后的评论,你需要两个if语句的AND条件.

NGinx无法做到这一点.

为实现这一点,我们将使用一个处理$test var的小技巧:

server {

  #...directives...

  error_page 404 /404.htm;

  if ($args != "") {                  # Test if there are some args       
     set $test A;
  }

  if (!-f /full/path/to/$arg_data) {  # Test if file in args doesn't exist
     set $test "${test}B";
  }

  if ($test = AB) {       # If there are some args AND if file doesn't exist
     return 404;
  }

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读