Welcome! Log In Create A New Profile

Advanced

Custom HTTP code in limit_except

June 07, 2018 12:58PM
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.
Subject Author Posted

Custom HTTP code in limit_except

5lava June 07, 2018 12:58PM

Re: Custom HTTP code in limit_except

5lava August 13, 2018 09:12PM



Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 175
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready