Nginx config if and try_files

I want to check query string in my config. If it matches load some page. If it isn't matches redirect it. So i write config like this:

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

But it doesn't work because i can't use all this directives inside if.

I try to use only one break in if, but it doesn't work too.

How i can do this?

Thanks.


Solution 1:

You just need to invert the logic in your if: (I'm also going to remove the proxy directives, since they have no effect here)

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

  try_files $uri /page_cache/$uri /page_cache/$uri/ /page_cache/$uri.html @puma;
}