ngx-php icon indicating copy to clipboard operation
ngx-php copied to clipboard

Does not work as expected with "try_files" directive

Open Rifle-Rex opened this issue 5 years ago • 4 comments

Hello. I had found some issues when I trying to use "try_files" directive. Below are my nginx.conf

init_worker_by_php_block {
    class a {
        static $a = 1;
        public static function add_one(){
            a::$a += 1;
        }
    }
}
server {
    server_name localhost;
    listen 808;
    
    location / {
        index index.html index.htm;
        try_files $uri $uri/ /index.php;
    }

    location /index.php{
        content_by_php_block {
            ngx_header_set("Content-Type", "text/html; charset=UTF-8");
            echo "12312\n";
            a::add_one();
            var_dump(a::$a);
        }
    }
    location /favicon.ico{
        return 403;
    }
}

when I open "http://localhost:808/p", A blank page with status code 200 was returned. But when I open "http://localhost:808/index.php", Everything was just fine. And the code in "content_by_php_block" is seen not be run in "http://localhost:808/p" because the a::$a is not add after the request. Is there anything I misunderstanding?

Rifle-Rex avatar Nov 24 '20 14:11 Rifle-Rex

@Rifle-Rex Thanks for your report, and I will track this problem.

rryqszq4 avatar Nov 27 '20 02:11 rryqszq4

I can confirm that try_files don't redirect if file not found. Only return a blank page with status 200.

And in my case, that I don't use a uri location redirection (best practice) only send a blank 200.

location / {
        index index.html index.htm;
        try_files $uri $uri/ @php-ngx;
}

location @php-ngx {
        content_by_php_block {
            ngx_header_set("Content-Type", "text/html; charset=UTF-8");
            echo "12312\n";
        }
}

This is necessary to use the Front Controller pattern, that use almost all frameworks.

An app can use a static dir for the static files, but some need to be in the root dir, like: favicon.ico, robots.txt, ...
And we need to also add all that uri locations. Normally is better add that uris, to configure cache, headers, log, ... but the try_files need to work too.

joanhey avatar Feb 10 '21 18:02 joanhey

@Rifle-Rex Thanks for your report, and I will track this problem.

Havent update this issue?

Qwoker avatar Aug 13 '23 22:08 Qwoker

I don't know, but I'll try to add tests for that first, and later try to fix it.

~PD: I need to read better this issue, and try it because it don't seems normal for me.~

joanhey avatar Aug 14 '23 00:08 joanhey