Try_files is broken with geoip?

Hi!

We experience problem: if inside location we use geoip variable
try_files
is not working and only root location is looked up for files.

So problem is reproducible with the following virtual server
configuration
(full configuration in attachment):

==================
geo $dontsecure {
default 0;
172.16.11.0/24 1;
127.0.0.0/24 1;
}

server {
listen 172.16.11.31 default_server;
listen 127.0.0.1 default_server;

access_log  /var/log/nginx/production-dvr.access_log main;
error_log  /var/log/nginx/production-dvr.error_log  debug;

location ~ /([^/]*?)(_lang_[0-9])?/.*\.ts {
  •   if ($dontsecure) {           set $reject_access 0;       }*
    
       root /tmp/test/;
       try_files /dir1$uri /dir2$uri =404;
    
    }
    }
    ==================

/tmp/test has follofing files:
/tmp/test/dir2/test/1.ts - file
/tmp/test/dir1/test - directory

Now if we request http://172.16.11.31/test/1.ts nginx returns 404 until
I
comment out selected by bold font lines. In debug log
(debug-log-nginx.txt
in attachment) it’s clear that try_files directive is doing nothing and
only /tmp/test/test/1.ts is looked up.

Now if I comment out this three bold lines in nginx works as expected
and
looks up /tmp/test/dir1/test/1.ts or if it not exist
/tmp/test/dir2/test/1.ts file.

nginx version: nginx/1.7.11
TLS SNI support enabled
configure arguments: --prefix=/usr --conf-path=/etc/nginx/nginx.conf
–error-log-path=/var/log/nginx/error_log --pid-path=/run/nginx.pid
–lock-path=/run/lock/nginx.lock --with-cc-opt=-I/usr/include
–with-ld-opt=-L/usr/lib64 --http-log-path=/var/log/nginx/access_log
–http-client-body-temp-path=/var/lib/nginx/tmp/client
–http-proxy-temp-path=/var/lib/nginx/tmp/proxy
–http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi
–http-scgi-temp-path=/var/lib/nginx/tmp/scgi
–http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --with-debug --with-ipv6
–with-pcre --with-http_flv_module --with-http_geoip_module
–with-http_mp4_module --with-http_secure_link_module
–with-http_realip_module
–add-module=external_module/nginx-rtmp-module-1.1.7
–add-module=external_module/nginx-push-stream-module-0.4.1
–with-http_ssl_module --without-mail_imap_module
–without-mail_pop3_module --without-mail_smtp_module --user=nginx
–group=nginx

Peter Volkov Wrote:
[…]
root /tmp/test/;
“/tmp/test/test/1.ts” → 404

/tmp/test has follofing files:
/tmp/test/dir2/test/1.ts - file
/tmp/test/dir1/test - directory

looks up /tmp/test/dir1/test/1.ts or if it not exist
/tmp/test/dir2/test/1.ts file.
[…]

I think you first need to sort out WHERE stuff is (root+uri) as your 404
is
valid against the other paths your have mentioned.

Posted at Nginx Forum:

On Mon, Mar 30, 2015 at 8:45 PM, itpp2012 [email protected] wrote:

/tmp/test/dir2/test/1.ts file.
[…]

I think you first need to sort out WHERE stuff is (root+uri) as your 404 is
valid against the other paths your have mentioned.

Sorry, I don’t understood your answer. Could you explain what you mean,
please?

That was an example configuration that is a fragment from more complex
one.
We use try_files /dir1$uri /dir2$uri =404; directive to lookup file
first
in dir1 and if file does not exist in dir1/temp/, then lookup in
dir2/temp.
so 1.ts could be either in dir1/test/1.ts or in dir2/test/1.ts. I think
it’s quite valid configuration. The problem is that it does not work
when
combined with geoip. So two questions are here:

  1. Are there any problems with this config
  2. Are there any workarounds/fixes for this problem?

On Mon, Mar 30, 2015 at 08:26:13PM +0300, Peter Volkov wrote:

Hi there,

We experience problem: if inside location we use geoip variable try_files
is not working and only root location is looked up for files.

That is working as expected for “if” inside “location”. It’s unrelated
to geoip.

Third item:

    # try_files wont work due to if

    location /if-try-files {
         try_files  /file  @fallback;

         set $true 1;

         if ($true) {
             # nothing
         }
    }

You’ll want to redesign whatever you are doing not to use “if” inside
“location” for anything other than “return” or effective equivalents.

f

Francis D. [email protected]

On Mon, Mar 30, 2015 at 10:18 PM, Francis D. [email protected]
wrote:

If is Evil… when used in location context | NGINX
if ($true) {
# nothing
}
}

You’ll want to redesign whatever you are doing not to use “if” inside
“location” for anything other than “return” or effective equivalents.

Hell, true! Thank you, Francis!

Peter Volkov Wrote:

/tmp/test/dir1/test - directory

looks up /tmp/test/dir1/test/1.ts or if it not exist
/tmp/test/dir2/test/1.ts file.
[…]

I think you first need to sort out WHERE stuff is (root+uri) as your 404
is
valid against the other paths your have mentioned.

Sorry, I don’t understood your answer. Could you explain what you
mean, please?

The 404 is about /tmp/test/test/1.ts (root+uri) however every other path
you
want to test for is not in there, so its going to fail always, maybe if
you
set root /tmp/; try_files could work.

We use try_files /dir1$uri /dir2$uri =404; directive to lookup file
first
in dir1 and if file does not exist in dir1/temp/, then lookup in
dir2/temp.

You could do a fallback ‘try_files $uri $uri/ @fallback;’ and then test
again with a different root setting.

Posted at Nginx Forum: