# "if" statement breaks try\_files?

**URL:** <https://www.ruby-forum.com/t/if-statement-breaks-try-files/213519>\
**Category:** NGINX\
**Created:** [November 4, 2011, 2:51am UTC](https://www.ruby-forum.com/t/if-statement-breaks-try-files/213519 "2011-11-04T02:51:50Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [November 4, 2011, 2:51am UTC](https://www.ruby-forum.com/t/if-statement-breaks-try-files/213519/1 "2011-11-04T02:51:50Z")

</div>

I’m seeing something really weird. If I put an “if” statement  
anywhere within the same block as a “try\_files” then it stops working.  
It always returns that the file exists even when it doesn’t.

I’m testing against nginx-1.0.9 on a Linux host.

This can be tested with this simple.conf:

worker\_processes 1;  
events { worker\_connections 1024; }  
http {  
server {  
listen 80;  
server\_name localhost;  
root /var/www;  
index index.html index.htm index.php;

```
  location ~ \.php($|/) {
     try_files doesnotexist =405;
     fastcgi_index index.php;
     fastcgi_pass unix:/tmp/php.socket;

     if ($uri) {}

     fastcgi_param SCRIPT_FILENAME /var/www$fastcgi_script_name;
     fastcgi_param HTTPS off;
     fastcgi_param QUERY_STRING $query_string;
     fastcgi_param REQUEST_METHOD $request_method;
     fastcgi_param CONTENT_TYPE $content_type;
     fastcgi_param CONTENT_LENGTH $content_length;
     fastcgi_param SCRIPT_NAME $fastcgi_script_name;
     fastcgi_param REQUEST_URI $request_uri;
     fastcgi_param DOCUMENT_URI $document_uri;
     fastcgi_param DOCUMENT_ROOT $document_root;
     fastcgi_param SERVER_PROTOCOL $server_protocol;
     fastcgi_param GATEWAY_INTERFACE CGI/1.1;
     fastcgi_param SERVER_SOFTWARE nginx;
     fastcgi_param REMOTE_ADDR $remote_addr;
     fastcgi_param REMOTE_PORT $remote_port;
     fastcgi_param SERVER_ADDR $server_addr;
     fastcgi_param SERVER_PORT $server_port;
     fastcgi_param SERVER_NAME $server_name;
     fastcgi_param REDIRECT_STATUS 200;
  }

```

}  
}

If that “if ($uri)” statement is left in it never returns the 405  
error when accessing whatever.php. If you comment out that “if” then  
it works as expected and always returns the 405 error.

Any idea what is going on here?

CR

---

<div class="post-metadata">

**Author:** ![chris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/958977/32.png) [@chris](https://www.ruby-forum.com/u/chris)\
**Post date:** [November 4, 2011, 7:14am UTC](https://www.ruby-forum.com/t/if-statement-breaks-try-files/213519/2 "2011-11-04T07:14:27Z")

</div>

See: [If is Evil… when used in location context | NGINX](http://wiki.nginx.org/IfIsEvil)

---

<div class="post-metadata">

**Author:** ![chris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/958977/32.png) [@chris](https://www.ruby-forum.com/u/chris)\
**Post date:** [November 4, 2011, 3:02pm UTC](https://www.ruby-forum.com/t/if-statement-breaks-try-files/213519/3 "2011-11-04T15:02:26Z")

</div>

On Fri, Nov 4, 2011 at 2:13 AM, Nginx U. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> See: [http://wiki.nginx.org/IfIsEvil](http://wiki.nginx.org/IfIsEvil)

Thanks, I should have known. I had read that but forgotten the most  
important parts.

Shame so many projects design their own language when so many proven  
works are out there, but I digress.

CR
