1
Ubuntu 16.04.5 LTS Xenial 4.18.8-x86_64-linode117
nginx version: nginx/1.10.3 (Ubuntu)
php v7.0.32-0ubuntu0.16.04.1`

/etc/nginx/sites-available/default

location /phpmyadmin {
        root /usr/share/;
        index index.php index.html index.htm;
        location ~ ^/phpmyadmin/(.+\.php)$ {
                try_files $uri =404;
                fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
                include fastcgi_params;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        }

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

No symlink at /var/www/html
Worx well....

But I can't seem to figure out how to change phpmyadmin url.
Tried creating a symlink whatever ->/usr/share/phpmyadmin or whatever ->/usr/share/phpmyadmin/ under /var/www/htmldoes not work,
Even if I change /etc/nginx/sites-available/default to:

location /whatever {
        root /usr/share/phpmyadmin/;

Created a symlink as mentioned above, restarted both nginx & php7.0-fpm still the new URL tried to download the page...

Looked up different resources on the net but they dont help:
digitalocean's guide
devanswers guide
As well as other here on SO but couldn't find a solution.
Ideas??

UPDATE
By Richard Smith's remark

location /whatever {
    alias /usr/share/phpmyadmin;
    index index.php index.html index.htm;
    if (!-e $request_filename) { rewrite ^ /phpmyadmin/index.php last; }
    # Secure phpMyAdmin Instance
    auth_basic "Admin Login";
    auth_basic_user_file /etc/nginx/.your_pass_file;
    client_max_body_size 68M;

    location ~ ^/phpmyadmin/(.+\.php)$ {
        if (!-f $request_filename) { return 404; }
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME 
        $document_root$fastcgi_script_name;
    }

    location ~* ^/phpmyadmin/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
        alias /usr/share/phpmyadmin;
    }
}
Isaac Bennetch
  • 11,830
  • 2
  • 32
  • 43
Jadeye
  • 3,551
  • 4
  • 47
  • 63
  • You will need to use `alias` instead of `root`. See [this answer](https://stackoverflow.com/questions/47319049/nginx-subdirectory-root-with-php/47332159#47332159) for an example. – Richard Smith Oct 12 '18 at 13:50
  • @RichardSmith I updated the question with the modified server block still `nginx` gives `403 Forbidden`... – Jadeye Oct 12 '18 at 21:36

1 Answers1

3

Try:

location ^~ /whatever {
    alias /usr/share/phpmyadmin;
    index index.php index.html index.htm;
    if (!-e $request_filename) { rewrite ^ /whatever/index.php last; }
    # Secure phpMyAdmin Instance
    auth_basic "Admin Login";
    auth_basic_user_file /etc/nginx/.your_pass_file;
    client_max_body_size 68M;

    location ~ \.php$ {
        if (!-f $request_filename) { return 404; }
        fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $request_filename;
    }
}

You need to use /whatever/index.php in the rewrite statement. The nested location blocks only need to match the end of the URI. The ^~ modifier avoids conflicts with other regular expression location blocks at the server block level. Use $request_filename in the SCRIPT_FILENAME. Your last nested location block appears to perform no function.

Avoid try_files with alias due to this issue. See this caution on the use of if.

Richard Smith
  • 45,711
  • 6
  • 82
  • 81