# Nginx parse var in if file exist statement error?

**URL:** <https://www.ruby-forum.com/t/nginx-parse-var-in-if-file-exist-statement-error/213444>\
**Category:** NGINX\
**Created:** [November 2, 2011, 11:10am UTC](https://www.ruby-forum.com/t/nginx-parse-var-in-if-file-exist-statement-error/213444 "2011-11-02T11:10:33Z")\
**Posts on this page:** 4\
**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 2, 2011, 11:10am UTC](https://www.ruby-forum.com/t/nginx-parse-var-in-if-file-exist-statement-error/213444/1 "2011-11-02T11:10:33Z")

</div>

Hi guys,

I found a stange bug in nginx 0.7.65, 0.8.54, and 1.0.6

Here is my setup of three machines

$ nginx -V  
nginx version: nginx/0.7.65  
TLS SNI support enabled  
configure arguments: --conf-path=/etc/nginx/nginx.conf  
–error-log-path=/var/log/nginx/error.log --pid-path=/var/run/nginx.pid  
–lock-path=/var/lock/nginx.lock  
–http-log-path=/var/log/nginx/access.log  
–http-client-body-temp-path=/var/lib/nginx/body  
–http-proxy-temp-path=/var/lib/nginx/proxy  
–http-fastcgi-temp-path=/var/lib/nginx/fastcgi --with-debug  
–with-http\_stub\_status\_module --with-http\_flv\_module  
–with-http\_ssl\_module --with-http\_dav\_module  
–with-http\_gzip\_static\_module --with-http\_realip\_module --with-mail  
–with-mail\_ssl\_module --with-ipv6  
–add-module=/build/buildd/nginx-0.7.65/modules/nginx-upstream-fair

$nginx -V  
nginx version: nginx/0.8.54  
built by gcc 4.2.4 (Ubuntu 4.2.4-1ubuntu4)  
configure arguments: --user=www-data --group=www-data  
–prefix=/usr/local/nginx --with-http\_stub\_status\_module

$ nginx -V  
nginx: nginx version: nginx/1.0.6  
nginx: TLS SNI support enabled  
nginx: configure arguments: --prefix=/etc/nginx/  
–sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf  
–error-log-path=/var/log/nginx/error.log  
–http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid  
–lock-path=/var/run/nginx.lock  
–http-client-body-temp-path=/var/cache/nginx/client\_temp  
–http-proxy-temp-path=/var/cache/nginx/proxy\_temp  
–http-fastcgi-temp-path=/var/cache/nginx/fastcgi\_temp  
–http-uwsgi-temp-path=/var/cache/nginx/uwcgi\_temp  
–http-scgi-temp-path=/var/cache/nginx/scgi\_temp --user=nginx  
–group=nginx --with-http\_ssl\_module --with-http\_realip\_module  
–with-http\_addition\_module --with-http\_sub\_module  
–with-http\_dav\_module --with-http\_flv\_module  
–with-http\_gzip\_static\_module --with-http\_random\_index\_module  
–with-http\_secure\_link\_module --with-http\_stub\_status\_module  
–with-mail --with-mail\_ssl\_module --with-file-aio --with-ipv6

So I have dir to store images, either jpg or png, the file name (image  
id) is unique. I want the URL to provide image id only, nginx to serve  
static jpg or png.

the config snippet is something like this:

server {  
listen 80;  
error\_log /var/log/nginx/error\_imglib.log debug;  
root /home/develop/image\_library;

location ~\* ^/img/small/(\d+)/?$ {  
set $fext ‘jpg’;  
if (!-f ‘/home/develop/image\_library/dump/$1-s.jpg’){  
set $fext png;  
}  
alias ‘/home/develop/image\_library/dump/$1-s.$fext’ ;  
}  
}

When running curl it returls something like this

$ curl “[http://127.0.0.1/img/small/1](http://127.0.0.1/img/small/1)”

404 Not Found
# 404 Not Found

* * *
nginx/1.0.6

Now the bug:

in three of my machines I set the error\_log level to “debug”, here is  
the output:

2011/11/02 14:01:09 [debug] 4194#0: \*24 http script capture: “1”  
2011/11/02 14:01:09 [debug] 4194#0: \*24 http script copy: “-s.”  
2011/11/02 14:01:09 [debug] 4194#0: \*24 http script var: “png”  
2011/11/02 14:01:09 [debug] 4194#0: \*24 http filename:  
“/home/steve/image\_library/dump/1-s.png1  
User-Agent”  
2011/11/02 14:01:09 [debug] 4194#0: \*24 add cleanup: 0833B2F0  
2011/11/02 14:01:09 [error] 4194#0: \*24 open()  
“/home/steve/image\_library/dump/1” failed (2: No such file or  
directory), client: 127.0.0.1, request: “GET /img/small/1 HTTP/1.1”

2011/11/01 23:00:53 [alert] 7793#0: \*38  
“/home/develop/image\_library/dump/1-s.jpgindex.html” is not a directory,  
request: “GET /img/small/1 HTTP/1.1”

2011/11/02 02:30:18 [error] 7666#0: \*2491 open()  
“/home/develop/image\_library/dump/1-s.pn” failed (2: No such file or  
directory), client: 127.0.0.1, request: “GET /img/small/1 HTTP/1.1”

2011/11/01 22:47:45 [error] 6740#0: \*24  
“/home/develop/image\_library/dump/1-s.jpgTP/1.1  
Hostindex.html” is not found (2: No such file or directory), request:  
“GET /img/small/1 HTTP/1.1”

So clearly somehow, nginx managed to mess HTTP headers into filename  
parser.

Can anyone help me? This is the weirdest bug I have ever encountered  
with nginx.

Posted at Nginx Forum:

> **[nginx parse var in if file exist statement error?](https://forum.nginx.org/read.php?2,217683,217683#msg-217683)**
>
> Hi guys, I found a stange bug in nginx 0.7.65, 0.8.54, and 1.0.6 Here is my setup of three machines $ nginx -V nginx version: nginx/0.7.65 TLS SNI support enabled configure arguments: --conf-path=/etc/nginx/nginx.conf --error-

---

<div class="post-metadata">

**Author:** ![est](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/74df32/32.png) [@est](https://www.ruby-forum.com/u/est)\
**Post date:** [November 2, 2011, 2:35pm UTC](https://www.ruby-forum.com/t/nginx-parse-var-in-if-file-exist-statement-error/213444/2 "2011-11-02T14:35:23Z")

</div>

On Wednesday 02 November 2011 14:09:59 est wrote:  
[…]

> root /home/develop/image\_library;
> 
> location ~\* ^/img/small/(\d+)/?$ {  
> set $fext ‘jpg’;  
> if (!-f ‘/home/develop/image\_library/dump/$1-s.jpg’){  
> set $fext png;  
> }  
> alias ‘/home/develop/image\_library/dump/$1-s.$fext’ ;  
> }  
> }

[…]

server {  
listen 80;  
error\_log /var/log/nginx/error\_imglib.log debug;  
root /home/develop/image\_library;

location ~\* ^/img/small/(\d+)/?$ {  
try\_files /dump/$1-s.jpg /dump/$1-s.png =404  
}  
}

wbr, Valentin V. Bartenev

---

<div class="post-metadata">

**Author:** ![est](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/74df32/32.png) [@est](https://www.ruby-forum.com/u/est)\
**Post date:** [November 3, 2011, 2:24am UTC](https://www.ruby-forum.com/t/nginx-parse-var-in-if-file-exist-statement-error/213444/3 "2011-11-03T02:24:58Z")

</div>

Oh thanks very much guys. try\_files worked well. I tried using try\_files  
before, but mistakenly wrote it as something like this

try\_files dump/$1-s.jpg dump/$1-s.png 404;

The correct working line should be like this

try\_files /dump/$1-s.jpg /dump/$1-s.png =404;

And I didn’t expect IF is evil. Suprise!

Thanks again.

Posted at Nginx Forum:

> **[Re: nginx parse var in if file exist statement error?](https://forum.nginx.org/read.php?2,217683,217730#msg-217730)**
>
> Hi guys, I found a stange bug in nginx 0.7.65, 0.8.54, and 1.0.6 Here is my setup of three machines $ nginx -V nginx version: nginx/0.7.65 TLS SNI support enabled configure arguments: --conf-path=/etc/nginx/nginx.conf --error-

---

<div class="post-metadata">

**Author:** ![est](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/e/74df32/32.png) [@est](https://www.ruby-forum.com/u/est)\
**Post date:** [November 2, 2011, 2:51pm UTC](https://www.ruby-forum.com/t/nginx-parse-var-in-if-file-exist-statement-error/213444/4 "2011-11-02T14:51:31Z")

</div>

Hello!

On Wed, Nov 02, 2011 at 06:09:59AM -0400, est wrote:

[…]

> So clearly somehow, nginx managed to mess HTTP headers into filename  
> parser.
> 
> Can anyone help me? This is the weirdest bug I have ever encountered  
> with nginx.

Valentin already replied how to do this properly. As for the bug  
itself, it’s documented here:

> **[GitHub - nginxinc/nginx-wiki: ARCHIVED -- Source for the now archived NGINX Wiki...](https://github.com/nginxinc/nginx-wiki)**
>
> ARCHIVED -- Source for the now archived NGINX Wiki section of https://www.nginx.com

Maxim D.
