NGINX - forbidden custom message

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:

On 3 Mar 2012 16h07 CET, [email protected] 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