0

I am trying to understand Nginx better. I came upon some lines of code where I think I understand what they do, but don't understand, why are they necessary.

Here is the phpmyadmin configuration snippet that I am using:

location /phpmyadmin {
       root /usr/share/;

       index index.php index.html index.htm;

       location ~ ^/phpmyadmin/(.+\.php)$ {
               try_files $uri =404;
               root /usr/share/;

               fastcgi_pass unix:/run/php/php7.2-fpm.sock;
               fastcgi_index index.php;
               fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
               include /etc/nginx/fastcgi_params;
       }

       location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
               root /usr/share/;
       }
}

I don't understand what purpose does the last location context serve. Every request that reaches it, already has the same root set in the wrapping context. Neither of the matched files would be matched by the other location, so it's not for a specificity war. Why do we need it then?

And why is the fastcgi_index index.php; line included? As far as I understand, index index.php index.html index.htm; line will internally redirect / to index.php and the request of / would never reach the PHP location, would it? Besides it seems that the / request wouldn't even match the location as there is no php. Am I missing something? I am aware that in this pargraph I was misusing / instead of the actual directory path. Is that the key here?

P.S. Unfourtunately I am not aware of the original source of this config, but that seems to be one of the most commonly suggested phpmyadmin configs, for example here, here and even on serverfault.

Džuris
  • 145
  • 1
  • 9

1 Answers1

0

Those fastcgi_index and the final location directives indeed have no effect. The index directive can be shortened as well. The root directive inside the first location doesn't change anything either

Besides, on fresh Nginx installations there is a fastcgi-php.conf file which sets the SCRIPT_FILENAME parameter as well, so these days this is perfectly enough:

location /phpmyadmin {
        root /usr/share;
        index index.php;

        location ~ ^/phpmyadmin/(.+\.php)$ {
                try_files $uri =404;

                fastcgi_pass unix:/run/php/php7.3-fpm.sock;
                include snippets/fastcgi-php.conf;
        }
}
Džuris
  • 145
  • 1
  • 9