1

I should get phppgadmin by example.com/phppgadmin, but something is going wrong. I could get it by example.com/ (see the comments in conf below). But if I'm trying to get phppgadmin by creating locationin nginx config I'm getting 404 not found. What am I doing wrong? Error.log is fine.

Here is the nginx conf:

server {

        listen 80 default_server;
        listen [::]:80 default_server;

        # Add index.php to the list if you are using PHP
        index index.php index.html index.htm index.nginx-debian.html;

        server_name example.com;

        #This path works. We are getting phppgadmin by example.com/ , 
        #but I need to get it by location (example.com/phppgadmin):
        #root /usr/share/phppgadmin;

        #This should work but it doesn't:
        location /phppgadmin/ {  

        root /usr/share;

        }

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                try_files $uri $uri/ =404;
        }

        location ~ \.php$ {
                include snippets/fastcgi-php.conf;
                fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
                include fastcgi_params;
        }
}
ekhodzitsky
  • 103
  • 1
  • 1
  • 10

1 Answers1

0

If you place root /usr/share; in the server block (where the original root statement was), the URI example.com/phppgadmin/ would work as expected.

But, it would also expose the entire contents of the /usr/share directory, which you may not want.

You can place the root statement inside a location but you need to include all of the directives necessary to process the request.

For example:

location ^~ /phppgadmin/ {  
    root /usr/share;
    try_files $uri $uri/ =404;

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

The ^~ modifier avoids any ambiguity with URIs ending with .php. See this document for details.

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