NGINX - forbidden custom message

António P. P. Almeida appa at perusio.net
Sat Mar 3 17:36:24 UTC 2012


On 3 Mar 2012 16h07 CET, nginx-forum at nginx.us 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



More information about the nginx mailing list