if condition and question

Igor Sysoev is at rambler-co.ru
Fri Sep 11 14:58:18 MSD 2009


On Thu, Sep 10, 2009 at 06:42:03PM +0200, Tomasz Pajor wrote:

> Hello,
> 
> When a request comes to the server i want to check if the $remote_addr 
> is in my list of addresses, if it is then serve the normal content if 
> not, then serve a static page.
> I tried something like this but it doesn't seem to work.
> 
> server {
>    listen 80;
>    server_name .app-domain.com;
>    error_log /var/log/nginx/app-error.log;
> 
>    root /disk0/vhosts/app/public;
>    charset utf-8;
> 
>    fastcgi_index index.php;
> 
>    location / {
>        if ($remote_addr !~ (x.x.x.x|z.z.z.z|y.y.y.y)) {
>            root /disk0/vhosts/blank;
>            break;
>        }
> 
>        try_files $uri $uri/ @fallback;
>        fastcgi_pass apps;
>        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
>        include fastcgi_params;
>    }
> 
>    location @fallback {
>        fastcgi_pass apps;
>        fastcgi_param SCRIPT_FILENAME $document_root/index.php;
>        include fastcgi_params;
>    }
> }

Try the following configuration:

http {
    geo $forbidden {
        default   1;

        x.x.x.x   0;
        z.z.z.z   0;
        ...
    }

    server {
        error_page  403 = /blank.html;

        location / {
            if ($forbidden) {
                return 403;
            }

            try_files $uri $uri/ @fallback;

            fastcgi_pass apps;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include fastcgi_params;
        }

        location = /blank.html {
            root /disk0/vhosts;
        }
    }


-- 
Igor Sysoev
http://sysoev.ru/en/





More information about the nginx mailing list