Rewriting

Bozhidara Marinchovska quintessence at bulinfo.net
Sat May 25 15:36:16 UTC 2013


Hello,

Because you placed root inside the location /.
As http://wiki.nginx.org/Pitfalls your root should also be placed in the 
server block.

Example:

   server {
                 listen          80;

                 root            /usr/local/www/test;

                 if ($remote_addr ~ '192.168.1.25')
                 {
                     rewrite ^/(.*)$ /unav/index.html break;
                 }

                 location / {
                 index index.html;
                 }

                 location /index.php {
                         ...
                 }

         }
}

Example content of index.html in /usr/local/www/test is "test"
Example content of index.html in /usr/local/www/test/unav is "unav dir"

Let we say your server is on 192.168.1.24:
- when you access http://192.168.1.24/index.html from host 192.168.1.25 
you will see in the browser index with content "unav dir".
- when you access http://192.168.1.24/index.php from host 192.168.1.25 
you will see in the browser index with content "unav dir".
- when you access http://192.168.1.24/index.html (or index.php) from 
another host for example 192.168.1.24 or 192.168.1.23 you will see the 
content of index.html (index.php) from your config. In the example is 
content "test" of index.html and phpinfo() in the index.php.


On 24.05.2013 09:01 ч., wishmaster wrote:
> Off course, you right. Thanks.
>
> if ($remote_addr !~ '190\.212\.201\.[0-9]{0,3}') {
>   rewrite ^/(.*)$ /unav/$1 break;
>   }
>
> location / {
>   ....
>   }
>   ........
>
>   But in log
>
>   2013/05/24 08:49:45 [error] 76017#0: *1910 open() "/usr/local/www/akbmaster/unav/unav/index.html" failed (2: No such file or directory), client: 190.212.201.198, server: akbmaster.server.com, request: "GET / HTTP/1.1", host: "akbmaster.server.com"
>
>    I see twice rewriting.
>    I have rewritten rule like this and this solved twice rewriting problem.
>   rewrite ^/([^/]*)$ /unav/$1 break;
>
>   Can you explain me why in my situation nginx have rewritten request twice?
>
> Cheers,
>
>   
>> Hello,
>>
>> http://nginx.org/en/docs/http/request_processing.html
>>
>> Because in this case you need to place your rule in server block if you
>> would like to be valid for all custom defined locations in your config
>> For example:
>>
>> server
>> {
>> listen 80;
>>
>> if ($remote_addr ~ '192.168.1.25')
>> {
>> return 403;
>> }
>>
>> location /
>> {
>> ...
>> }
>>
>> location ~ \.php$
>> {
>> ...
>> }
>>
>> }
>>
>> On 23.05.2013 23:11 ч., wishmaster wrote:
>>> Hi,
>>> I use opencart with nginx+php-fpm. Sometimes it is necessary to redirect all clients, except admin (190.212.201.0/24), to "Service unavailable" page which is simple index.html file with logo, background image and some text, located in /unav directory.
>>> Below  some of nginx.conf
>>>
>>>
>>> location / {
>>>
>>> if ($remote_addr !~ '190\.212\.201\.[0-9]{0,3}') {
>>> rewrite ^/(.*)$ /unav/$1 break;
>>> return 403;
>>> }
>>> try_files $uri $uri/ @opencart;
>>> }
>>>
>>> location ^~ /unav {
>>> }
>>>
>>> location @opencart {
>>> rewrite ^/(.+)$ /index.php?_route_=$1 last;
>>> }
>>>
>>> [...skipped...]
>>>
>>> location ~ \.php$ {
>>>
>>>            try_files      $uri =404;
>>>            fastcgi_read_timeout 60s;
>>>            fastcgi_send_timeout 60s;
>>>            include           myphp-fpm.conf;
>>>
>>>                       }
>>>
>>> Problem  is in rewriting.
>>> This rule
>>>
>>> rewrite ^/(.*)$ /unav/$1 break;
>>>
>>> rewrite ONLY http://mysite.com/ request but in case http://mysite.com/index.php rewrite is none and request processed by location ~ \.php$ rule. Why??
>>>
>>> Thanks!
>>>
>>>
>>> _______________________________________________
>>> nginx mailing list
>>> nginx at nginx.org
>>> http://mailman.nginx.org/mailman/listinfo/nginx



More information about the nginx mailing list