# Custom 403 error page quirk

**URL:** <https://www.ruby-forum.com/t/custom-403-error-page-quirk/198425>\
**Category:** NGINX\
**Created:** [November 1, 2010, 8:03am UTC](https://www.ruby-forum.com/t/custom-403-error-page-quirk/198425 "2010-11-01T08:03:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dobai-Pataky\_BSSSSl](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/77aa72/32.png) [@Dobai-Pataky\_BSSSSl](https://www.ruby-forum.com/u/Dobai-Pataky_BSSSSl)\
**Post date:** [November 1, 2010, 8:03am UTC](https://www.ruby-forum.com/t/custom-403-error-page-quirk/198425/1 "2010-11-01T08:03:57Z")

</div>

Hi

I have been trying to imlement a custom 403 error page - to be  
triggered when the request method is not in HEAD,GET,POST. I started  
with something like this (note I was specifically triggering the 403  
error for GET requests for testing):

root /htdocs

location = /403.html {  
internal;  
}

error\_page 403 = /403.html;

if ($request\_method !~ ^(POST)$ ) {  
return 403;  
}

```
location / {
    index index.html;
}

```

For some reason, the above config _always_ caused nginx internal 403  
error page to be generated - instead of my custom page at  
/htdoc/403.html.

After scouring forums and whatnot, I found a suggestion, which is to put  
the if() inside the location block - like:

```
location / {
    index index.html;

     if ($request_method !~ ^(POST)$ ) {
         return 403;
    }
}

```

Doing this and everything seemed to work. I just don’t understand why -  
can someone explain this?

Cheers

Posted at Nginx Forum:

> **[custom 403 error page quirk](https://forum.nginx.org/read.php?2,146462,146462#msg-146462)**
>
> Hi I have been trying to imlement a custom 403 error page - to be triggered when the request method is not in HEAD,GET,POST. I started with something like this (note I was specifically triggering the 403 error for GET requests for testing): root...

---

<div class="post-metadata">

**Author:** ![megamic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecae2f/32.png) [@megamic](https://www.ruby-forum.com/u/megamic)\
**Post date:** [November 1, 2010, 9:34am UTC](https://www.ruby-forum.com/t/custom-403-error-page-quirk/198425/2 "2010-11-01T09:34:14Z")

</div>

Hello!

On Mon, Nov 01, 2010 at 03:02:21AM -0400, megamic wrote:

> ```
> internal;
> }
> 
> if ($request_method !~ ^(POST)$ ) {
> return 403;
> }
> }
> 
> ```
> 
> Doing this and everything seemed to work. I just don’t understand why -  
> can someone explain this?

> **[server\_name '\_' and custom error\_page](http://mailman.nginx.org/pipermail/nginx/2010-October/023353.html)**

Maxim D.

---

<div class="post-metadata">

**Author:** ![megamic](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/ecae2f/32.png) [@megamic](https://www.ruby-forum.com/u/megamic)\
**Post date:** [November 1, 2010, 10:35am UTC](https://www.ruby-forum.com/t/custom-403-error-page-quirk/198425/3 "2010-11-01T10:35:06Z")

</div>

## Maxim D. Wrote:

> HEAD,GET,POST. I started
> 
> > error\_page 403 = /403.html;  
> > caused nginx internal 403
> 
> 53.html  
> Thanks for this link, it does seem to explain things if I understood it  
> correctly. So basically what was going on in this:

1. The return 403 in the server{} body caused 403 to be return on every  
internal redirect where method != POST
2. The first one was the actual request, which returned 403 as the  
method was not POST
3. We then tried to access location /403.html, however this was also  
checked against the 403 rule, and as the method was != POST, we return  
another 403
4. As you said in the last response…“wow, we’ve already done  
error\_page redirection and  
this resulted in another error, return default error page”

So…I guess my next question would be how to elegantly apply a rule  
like this against all locations (or a number of selected locations),  
without having to copy it into the location{} body for each one?

Cheers

> Maxim D.
> 
> * * *
> 
> nginx mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [mailman.nginx.org archive](http://nginx.org/mailman/listinfo/nginx)

Posted at Nginx Forum:  
[http://forum.nginx.org/read.php?2,146462,146489#msg-146489](http://forum.nginx.org/read.php?2,146462,146489#msg-146489)
