Custom HTTP code in limit_except

5lava nginx-forum at forum.nginx.org
Thu Jun 7 16:58:18 UTC 2018


I'd like to find an elegant and efficient solution to redirect GET and HEAD
requests using code 301, but requests with other methods — using code 308.
Intuitively I wrote this:

location /foo {
  limit_except GET { return 301 /bar; }
  return 308 /bar;
}

But allowed context for "return" are "server", "location", and "if", so
nginx won't start (error: "return" directive is not allowed here).

Another approach would be using "if" e.g.:

location /foo {
  if ( $request_method = GET ) { return 301 /bar; }
  if ( $request_method = HEAD ) { return 301 /bar; }
  return 308 /bar;
}

But this doesn't seem quite elegant (regex could make it look a bit nicer
but less efficient).

I'm wondering if anyone can suggest a better idea?

And, if nginx developers are reading this, is "if ( $request_method = GET )"
equivalent to "limit_except GET", performance-wise?
Also, just wondering if there are some technical limitations that prevent
making "return" work inside "limit_except" block? Currently only "deny"
works in "limit_except" but it's only capable of returning 403.

Thank you.

Posted at Nginx Forum: https://forum.nginx.org/read.php?2,280084,280084#msg-280084



More information about the nginx mailing list