Nginx downloads a file instead of going to index once you go to a folder url

June 30, 2019 240 views
Nginx PHP Ubuntu 18.04

I'm trying to setup phpmyadmin on my new ubuntu server running nginx. When I do domain.com/phpmyadmin/ it downloads a file consisting of php code etc while if I do domain.com/phpmyadmin/index.php it loads in correct. Got no idea why. This is my server block:

server {
    listen 80;
    listen [::]:80;

    root /var/www/domain.com/html;
    index index.php index.html index.htm index.nginx-debian.html;

    server_name domain.com;

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.2-fpm.sock;
    }

    location ~ /\.ht {
        deny all;
    }

    location / {
        try_files $uri/ $uri =404;
    }
}

and I have set '''cgi.fix_pathinfo=0'''.

4 Answers
DreamplaySE June 30, 2019
Accepted Answer

Didn't know file downloads were cached. Forgot to clear cache. Now I feel dumb cause ofcourse downloads are cached and I'm just dumb.

location / {
        try_files $uri/ $uri =404;
    }

is actually

location / {
        try_files $uri $uri/ =404;
    }

Just tried to switch them around. Don't know why just had a thought that that might matter.

Oh I just realized it's because I've linked phpmyadmin thru a symlink. Any way to fix the problem or is the best way to set root to the php folder instead?

@DreamplaySE

You can use root or alias within the location block to set the path (though you should only use one or the other, not both)--alias is often preferred (root is generally used to set the high-level path, outside of location blocks). I've provided a few examples below.

For root, you'll want to set the base path sans phpmyadmin. NGINX will look within the root path for the directories and files being requested. For example:

location ^~ /phpmyadmin {
    root /var/www;
    try_files $uri $uri/ =404;
}

When /phpmyadmin is requested, the path updates to reflect the request. For example:

/var/www/phpmyadmin

-

Unlike root, alias can only be used within location blocks and alias overrides the default root directory. For alias, you'll want to use the full path to where phpmyadmin is located (the physical directory, not the symlink). For example:

location ^~ /phpmyadmin {
    alias /var/www/phpmyadmin;
    try_files $uri $uri/ =404;
}

With alias, NGINX does not postfix /phpmyadmin to the end of the path. In many ways, this functions much like a symlink would, though you're linking to the physical directory where phpmyadmin is.

Have another answer? Share your knowledge.