nginx: how to serve /index.php of subfolders?

Viewed 104

I have a legacy php websiste that has been migrated to a new server with nginx (and php 7.4)

My nginx has near only this

server {
    listen 80;

    server_name domain.tld;

    root /var/www/domain.tld;

    error_log   /var/log/nginx/domain.tld-error.log  warn;
    access_log  /var/log/nginx/domain.tldt-access.log combined;

    client_body_buffer_size     10M;
    client_max_body_size        10M;

    location / {
        try_files $uri /index.php?$args;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/run/php/php7.4-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
        fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
    }
}

If I access to mydomain.tld it works, it serves correctly mydomain.tld/index.php

but If I access mydomain.tld/subfolder or mydomain.tld/subfolder/ it doesn't serve mydomain.tld/subfolder/index.php as I expect

What am I doing wrong?

1 Answers

I resolved modifiying the suggestion of Daniel W

I moved the location / block under location ~ \.php$

But instead of remove the initial / I prepended also $uri

location ~ \.php$ {
    try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass unix:/run/php/php7.4-fpm.sock;
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_param   SCRIPT_FILENAME    $document_root$fastcgi_script_name;
    fastcgi_param   SCRIPT_NAME        $fastcgi_script_name;
}


location / {
    index index.php;
    try_files $uri $uri/index.php?$args;
}

If I will find some pitfalls I will update my answer

Related