-e vs -f and -d

Maxim Dounin mdounin at mdounin.ru
Mon Dec 11 16:10:25 UTC 2017


Hello!

On Mon, Dec 11, 2017 at 09:51:51AM -0500, drook wrote:

> Hi,
> 
> Considering that I don't have symbolic links why do these configs work
> differently ?
> 
> ===config one===
> if (!-f $request_filename) {
>     rewrite ^/(.*)$ /init.php;
> }
> if (!-d $request_filename) {
>     rewrite ^/(.*)$ /init.php;
> }
> ===config one===
> 
> This one above works, rewrite happens.

This will rewrite all requests, since you have two independant 
cases when a rewrite happens:

- rewrite if the requested resource is not a file;
- rewrite if the requested resource is not a directory;

Since no resource can be a file and a directory at the same time, 
rewrite always happens.  That is, this configuration will rewrite 
anything, including requests to existing files and directories.

> Being changed to this it stops working, all other lines left intact:
> ===config two===
> if (!-e $request_filename) {
>     rewrite ^/(.*)$ /init.php;
> }
> ===config two===

This will not rewrite resources which are either a file, or a 
directory.  Everything else will be rewritten.  That is, this 
configuration will not rewrite requests to existing files and 
directories.

See here for more information on rewrite module directives and 
their execution:

http://nginx.org/en/docs/http/ngx_http_rewrite_module.html

In particular, "rewrite_log on;" might be helpful if you want to 
better understand what happens in your configuration.

-- 
Maxim Dounin
http://mdounin.ru/


More information about the nginx mailing list