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

前端之家收集整理的这篇文章主要介绍了nginx – 如果作为参数传递的文件名没有退出,则重定向到404前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我在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;
  }

}
原文链接:https://www.f2er.com/nginx/435234.html

猜你在找的Nginx相关文章