13

I'm trying to set up an alias so when someone accesses /phpmyadmin/, nginx will pull it from /home/phpmyadmin/ rather than from the usual document root. However, everytime I pull up the URL, it gives me a 404 on all items not pulled through fastcgi. fastcgi seems to be working fine, whereas the rest is not. strace is telling me it's trying to pull everything else from the usual document root, yet I can't figure out why. Can anyone provide some insight?

Here is the relevant part of my config:

location ~ ^/phpmyadmin/(.+\.php)$
{
        include fcgi.conf;
        fastcgi_index index.php;
        fastcgi_pass  unix:/tmp/php-cgi.sock;
        fastcgi_param SCRIPT_FILENAME /home$fastcgi_script_name;
}

location /phpmyadmin
{
        alias /home/phpmyadmin/;
}
ajmeese7
  • 107
  • 5
Rob
  • 2,303
  • 9
  • 31
  • 50
  • As a long-time apache user new to nginx, I find this really strange and unnecessarily complicated. I spent several hours looking through documentation to try to understand why. Can someone please explain this behaviour? Aliases should be just that - an alias to a location. – tudor -Reinstate Monica- Oct 28 '14 at 04:08
  • As far as I understand it, aliases are "just that", although note that if you omit the trailing `/` in the alias *and* the `location` directive, requesting `/phpmyadminfile` will actually get you `/home/phpmyadminfile`. As for why the config in the question doesn't work, it looks correct to me, so I suspect there's another `location` directive not shown which is matching when not intended. In this case, as the `alias` ends with the `location` prefix, [the nginx docs suggest using `root /home;` instead](https://nginx.org/en/docs/http/ngx_http_core_module.html#alias). – Scott Stevens Sep 14 '17 at 12:42

4 Answers4

15

Figured out a way. I'm not sure if it's the BEST, but it's certainly working right now.

Here's what I did:

location ~ ^/phpmyadmin/(.*)$
{
        alias /home/phpmyadmin/$1;
}
ajmeese7
  • 107
  • 5
Rob
  • 2,303
  • 9
  • 31
  • 50
2

I'm not sure but have you tried writing it this way:

location /phpmyadmin/
{
        alias /home/phpmyadmin/;
}

Also, what's the URI from which you are trying to access it?

As far as I understand it, you have to use the URI

/home/phpmyadmin/

and not just

/phpmyadmin/

You can read more about it here: http://wiki.nginx.org/HttpCoreModule

ajmeese7
  • 107
  • 5
Black-Pixel
  • 121
  • 1
  • 3
  • Thank you for an answer, at least. But yeah. I've tried that. Same thing. – Rob Apr 02 '12 at 20:31
  • Maybe you have to add the alias to the other location. You could compare your config to the ones there: http://serverfault.com/questions/223028/alias-using-nginx-causing-phpmyadmin-login-endless-loop – Black-Pixel Apr 02 '12 at 20:40
1

Try

location ^~ /phpMyAdmin/
{
    alias /home/phpmyadmin/;

    location ~ \.php$
    {
        include fcgi.conf;
        fastcgi_index index.php;
        fastcgi_pass  unix:/tmp/php-cgi.sock;
        fastcgi_param SCRIPT_FILENAME /home$fastcgi_script_name;
    }
}
kervin
  • 211
  • 2
  • 7
1

The reason this doesn't work is the server is choosing the regex based location and not the prefix based location that has the alias in it.

It will only choose one. There is an operator you can use to get a prefix based location to outrank a regex one, but then you'll find php won't work. The solution will be one like kervin's answer where the PHP regex is nested inside the location with the alias directive.

I suspect however his SCRIPT_FILENAME setting may not work there since there is different capitalisation on the URL and the aliased directory.

thomasrutter
  • 2,437
  • 1
  • 25
  • 34