# NGINX - forbidden custom message

**URL:** <https://www.ruby-forum.com/t/nginx-forbidden-custom-message/217509>\
**Category:** NGINX\
**Created:** [March 3, 2012, 4:07pm UTC](https://www.ruby-forum.com/t/nginx-forbidden-custom-message/217509 "2012-03-03T16:07:50Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [March 3, 2012, 4:07pm UTC](https://www.ruby-forum.com/t/nginx-forbidden-custom-message/217509/1 "2012-03-03T16:07:50Z")

</div>

sorry for being so direct but this is what I need to do…

in one of my virtual hosts I need to show a custom message based on  
user’s IP.

for example…

if user comes from 1.0.0.0/8 or 12.0.0.0/8 it get a custom message/page  
yet if user comes from anywhere else he’ll be able to use site normally

how would I accomplish that?

Posted at Nginx Forum:

> **[NGINX - forbidden custom message](https://forum.nginx.org/read.php?2,223295,223295#msg-223295)**
>
> sorry for being so direct but this is what I need to do... in one of my virtual hosts I need to show a custom message based on user's IP. for example... if user comes from 1.0.0.0/8 or 12.0.0.0/8 it get a custom message/page yet if user comes from...

---

<div class="post-metadata">

**Author:** ![alexus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/dc4da7/32.png) [@alexus](https://www.ruby-forum.com/u/alexus)\
**Post date:** [March 3, 2012, 6:36pm UTC](https://www.ruby-forum.com/t/nginx-forbidden-custom-message/217509/2 "2012-03-03T18:36:55Z")

</div>

On 3 Mar 2012 16h07 CET, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> sorry for being so direct but this is what I need to do…
> 
> in one of my virtual hosts I need to show a custom message based on  
> user’s IP.
> 
> for example…
> 
> if user comes from 1.0.0.0/8 or 12.0.0.0/8 it get a custom  
> message/page yet if user comes from anywhere else he’ll be able to  
> use site normally

> how would I accomplish that?

- 1st option

At the http level:

geo $forbidden\_client {  
default 0;  
1.0.0.0/8 1;  
12.0.0.0/8 1;  
}

server {  
# …

```
error_page 418 /custom_message.html;

if ($forbidden_client) {
    return 418;
}

```

}

- 2nd option

server {  
# …

```
error_page 403 /custom_message.html;

deny 1.0.0.0/8;
deny 12.0.0.0/8;
allow all;

```

}

— appa
