Nginx 配置 if 和 try_files

Fal*_*con 5 nginx

我想在我的配置中检查查询字符串。如果匹配加载某个页面。如果不匹配,则重定向它。所以我这样写配置:

...
location / {
    if ($args ~ "api_url") {    
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        index  index.html;
        try_files $uri /page_cache/$uri /page_cache/$uri/ /page_cache/$uri.html @puma;
        break;
    }
    rewrite ^ http://domain.com permanent;
}
...
Run Code Online (Sandbox Code Playgroud)

但它不起作用,因为我不能在 if 中使用所有这些指令。

我尝试break在 if 中只使用一个,但它也不起作用。

我怎么能做到这一点?

谢谢。

kol*_*ack 5

您只需要反转 if 中的逻辑即可:(我还将删除代理指令,因为它们在这里不起作用)

location / {
  if ($arg_api_url != '') {
    return 301 http://domain.com/;
  }

  try_files $uri /page_cache/$uri /page_cache/$uri/ /page_cache/$uri.html @puma;
}
Run Code Online (Sandbox Code Playgroud)