Opened 10 years ago

Closed 10 years ago

#733 closed defect (duplicate)

wrong internal redirect using alias and try_files

Reported by: Raul Predescu Owned by:
Priority: minor Milestone:
Component: nginx-core Version:
Keywords: try_files alias Cc:
uname -a: Linux cloud-server-13 2.6.32-504.8.1.el6.x86_64 #1 SMP Wed Jan 28 21:11:36 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
nginx -V: nginx version: nginx/1.0.15
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-11) (GCC)
TLS SNI support enabled
configure arguments: --prefix=/usr/share/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 --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/var/run/nginx.pid --lock-path=/var/lock/subsys/nginx --user=nginx --group=nginx --with-file-aio --with-ipv6 --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_xslt_module --with-http_image_filter_module --with-http_geoip_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_degradation_module --with-http_stub_status_module --with-http_perl_module --with-mail --with-mail_ssl_module --with-debug --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic' --with-ld-opt=-Wl,-E

Description

Hey guys
I just discovered a very strange bug ( or so i think )

Config looks like this:

server {
    listen 80;
    server_name mydomain.com;
    root /home/mydomain.com/public;

    index index.php;

    charset utf-8;

    location ^~ /app1 {
        alias /home/mydomain.com/app1/public;
        try_files $uri $uri/ /app1/index.php?$query_string;

        location ~ \.php$ {
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_param SCRIPT_FILENAME $request_filename;
            fastcgi_index index.php;
            include fastcgi_params;
        }
    }

    location ^~ /app2 {
        alias /home/mydomain.com/app2/public;
        try_files $uri $uri/ /app2/index.php?$query_string;

        location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $request_filename;
        fastcgi_index index.php;
        include fastcgi_params;
        }
    }

    location / {
        try_files $uri $uri/ /index.php?$query_string;
	    location ~ \.php$ {
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $request_filename;
        }
    }

A request to mydomain.com/test?x=1 will FAIL and go to mydomain.com/index.php?x=1
Debug log:

2015/03/15 07:01:36 [debug] 21909#0: *1 generic phase: 0
2015/03/15 07:01:36 [debug] 21909#0: *1 rewrite phase: 1
2015/03/15 07:01:36 [debug] 21909#0: *1 test location: "/"
2015/03/15 07:01:36 [debug] 21909#0: *1 test location: "favicon.ico"
2015/03/15 07:01:36 [debug] 21909#0: *1 test location: "app2"
2015/03/15 07:01:36 [debug] 21909#0: *1 test location: "app1"
2015/03/15 07:01:36 [debug] 21909#0: *1 test location: ~ "\.php$"
2015/03/15 07:01:36 [debug] 21909#0: *1 using configuration "/app1"
2015/03/15 07:01:36 [debug] 21909#0: *1 http cl:-1 max:1048576
2015/03/15 07:01:36 [debug] 21909#0: *1 rewrite phase: 3
2015/03/15 07:01:36 [debug] 21909#0: *1 post rewrite phase: 4
2015/03/15 07:01:36 [debug] 21909#0: *1 generic phase: 5
2015/03/15 07:01:36 [debug] 21909#0: *1 generic phase: 6
2015/03/15 07:01:36 [debug] 21909#0: *1 generic phase: 7
2015/03/15 07:01:36 [debug] 21909#0: *1 generic phase: 8
2015/03/15 07:01:36 [debug] 21909#0: *1 access phase: 9
2015/03/15 07:01:36 [debug] 21909#0: *1 access phase: 10
2015/03/15 07:01:36 [debug] 21909#0: *1 post access phase: 11
2015/03/15 07:01:36 [debug] 21909#0: *1 try files phase: 12
2015/03/15 07:01:36 [debug] 21909#0: *1 http script var: "/app1/test"
2015/03/15 07:01:36 [debug] 21909#0: *1 trying to use file: "/test" "/home/mydomain.com/app1/public/test"
2015/03/15 07:01:36 [debug] 21909#0: *1 http script var: "/app1/test"
2015/03/15 07:01:36 [debug] 21909#0: *1 trying to use dir: "/test" "/home/mydomain.com/app1/public/test"
2015/03/15 07:01:36 [debug] 21909#0: *1 http script copy: "/app1/index.php?"
2015/03/15 07:01:36 [debug] 21909#0: *1 http script var: "a=1"
2015/03/15 07:01:36 [debug] 21909#0: *1 trying to use file: "/index.php?a=1" "/home/mydomain.com/app1/public/index.php?a=1"
2015/03/15 07:01:36 [debug] 21909#0: *1 internal redirect: "/index.php?a=1"
2015/03/15 07:01:36 [debug] 21909#0: *1 rewrite phase: 1
2015/03/15 07:01:36 [debug] 21909#0: *1 test location: "/"
2015/03/15 07:01:36 [debug] 21909#0: *1 test location: "favicon.ico"
2015/03/15 07:01:36 [debug] 21909#0: *1 test location: "robots.txt"
2015/03/15 07:01:36 [debug] 21909#0: *1 test location: ~ "\.php$"
2015/03/15 07:01:36 [debug] 21909#0: *1 using configuration "\.php$"
2015/03/15 07:01:36 [debug] 21909#0: *1 http cl:-1 max:1048576
2015/03/15 07:01:36 [debug] 21909#0: *1 rewrite phase: 3
2015/03/15 07:01:36 [debug] 21909#0: *1 post rewrite phase: 4
2015/03/15 07:01:36 [debug] 21909#0: *1 generic phase: 5
2015/03/15 07:01:36 [debug] 21909#0: *1 generic phase: 6
2015/03/15 07:01:36 [debug] 21909#0: *1 generic phase: 7
2015/03/15 07:01:36 [debug] 21909#0: *1 generic phase: 8
2015/03/15 07:01:36 [debug] 21909#0: *1 access phase: 9
2015/03/15 07:01:36 [debug] 21909#0: *1 access phase: 10
2015/03/15 07:01:36 [debug] 21909#0: *1 post access phase: 11
2015/03/15 07:01:36 [debug] 21909#0: *1 try files phase: 12
2015/03/15 07:01:36 [debug] 21909#0: *1 posix_memalign: 0000000000E780E0:4096 @16
2015/03/15 07:01:36 [debug] 21909#0: *1 http init upstream, client timer: 0
....

As you can see, it founded the file, but the internal redirect stripped the path.
As a workaround i had to add
try_files $uri $uri/ /app1/app1/index.php?$query_string;
This ... works ! /app1 is stripped once => valid path :)

I've tested this on nginx1.0.15 centos 6 x64
i manually compiled 1.7.10 and latest stable 1.6.2 from nginx repo
All 3 had the same behaviour

Thanks

Change History (1)

comment:1 by Sergey Kandaurov, 10 years ago

Resolution: duplicate
Status: newclosed

Closing this as duplicate of ticket #97.

Note: See TracTickets for help on using tickets.