Rewriting

Francis Daly francis at daoine.org
Sun May 26 09:23:42 UTC 2013


On Sat, May 25, 2013 at 02:40:37PM +0300, wishmaster wrote:

[Back to the list]

Hi there,

> > I suspect that the final logic will be something like:
> > 
> > if this is not an admin address
> > if the request is for /unav/something, then serve the file
> > else return http 503 with the content corresponding to /unav/index.html
> > else
> > give full normal access to the site
>  
>   My logic is: for all requests  (from not an admin) like http://example.org/ use rewriting and show for clients simple temp html page.

I think that that statement is not specific enough for what you actually
want.

Because your html page "/unav/index.html" will include an image
"/unav/img.png", and you want that not to count as "all requests".

> But I think, I have solved it :)

If this does what you want it to, that's good.

When I tested, I saw some problems.

>  if ($remote_addr ~ '_admin_IP_') {
>                 rewrite ^/(.*)$ /unav/$1 last;
>         }
> 
>         location ^~ /unav/ {
>         try_files $uri $uri/ /index.html;
>         }

That will serve the content of /unav/index.html for all requests (unless
you have an /unav/unav/img.png, for example), but it goes through lots
of rewrites to get there.

>         location / {
> 
>                 try_files $uri $uri/ @opencart;
>         }
> 
>         location @opencart {
>                 rewrite ^/(.+)$ /index.php?_route_=$1 last;
>         }
> 
>    ... and so on ..... 

My suggestion:

use "geo" or "map" or even "if" to set a variable $site_looks_down,
which is "1" for users and "0" for admins.

if that variable is true, do nothing special for /unav/* urls, and return
503 for the rest.

have the 503 error return the content of /unav/index.html

have /unav/index.html include links like /unav/img.png


Overall, this is something like:

  http {
    geo $site_looks_down {
      default 1;
      127.0.0.3 0;
    }
    server {
      if ($site_looks_down) {
        rewrite ^/unav/ $uri last;
        return 503;
      }
      error_page 503 /unav/index.html;
      location ^~ /unav/ {
      }
      # extra location{}s and server-level config here
    }
  }


Now you can do things like

  curl -i http://127.0.0.1/unav/img.png
  curl -i http://127.0.0.3/unav/img.png

and you should see the same image response, while things like

  curl -i http://127.0.0.1/sample/
  curl -i http://127.0.0.3/sample/

should give you different responses -- one "503 unavailable", and one
correct (maybe 404, maybe useful content).


Change the "default" value to 0, or (better) just remove the if() block,
to make the site available to all again.

See documentation at things like http://nginx.org/r/geo for the syntax.

	f
-- 
Francis Daly        francis at daoine.org



More information about the nginx mailing list