Different locations based on $request_method

Hello, all,

I would like to configure nginx to return a file if the request method
is
GET. But, if the request method is PUT or POST, I’d like for the back
end to
handle it. So, ideally, something like this:

location / GET {
  try_files $uri $uri/index.html $uri.html @missing;
}

location / PUT {
  proxy_pass http://sinatra;
  ....
}

location @missing {
  return 404;
}

I understand that that particular syntax isn’t possible. Is something
like
this the best way to do it?

location / {

  if ($request_method = PUT) {
    proxy_pass http://sinatra;
    ....
    break;
  }

  try_files $uri $uri/index.html $uri.html @missing;
}

location @missing {
  return 404;
}

I’m aware that if==bad, but not sure if there’s a better way to redirect
traffic based on the request method?

Thanks for any help,

Aaron

To follow up on my own question:

Maxim D., in response to another question in the forum, recommended
this
version to get a very similar effect and avoid the if:

location / {
  # static handler returns 405 for POST and PUT requests to a static

file

  error_page 405 = @sinatra;
  try_files $uri $uri/index.html $uri.html @sinatra;
}

location @sinatra {
  ...
  proxy_pass http://app_servers;
}