Nginx的configuration如果和try_files

我想检查我的configuration查询string。 如果它匹配加载一些页面。 如果不匹配,redirect它。 所以我写这样的configuration:

... 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; } ... 

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

我试图只使用一个break ,但它不工作。

我怎么能做到这一点?

谢谢。

你只需要在你的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; }