0

I want to configure a location for the phpinfo.php file with /phpinfo and another one for phpPgAdmin with /phppgadmin. My server root directory is at /usr/share/nginx/html/, in there I got an index.html and the phpinfo.php file and the phpPgAdmin is located at /usr/share/phppgadmin with his index.php file inside.

This is my default.conf file:

server {
    listen       80;
    server_name  localhost;
    root /usr/share/nginx/html;
    index index.html index.htm index.php;

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/run/php/php7.3-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include       fastcgi_params;
    }

    location /phpinfo {
        index phpinfo.php;
    }

    location /phppgadmin {
        alias /usr/share/phppgadmin;
    }
}

I put a location block for each but none works.

Basically what I want is to configure all this to go to my php info with http://localhost/phpinfo and to my phpPgAdmin database manager with http://localhost/phppgadmin.

0

Lets first take this minimal working example:

server {
    listen   80;
    server_name  test.sc;
    root /usr/share/nginx/html;
    index index.html index.htm index.php;

    location /phpinfo {
        alias /usr/share/phppgadmin/;
        index phpinfo.php;

    }

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include       fastcgi_params;
    }

}

Here the problem is, that by using

fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

FPM gets passed /usr/share/nginx/html/phpinfo.php for processing, since the with the "root" directive /usr/share/nginx/html was defined. This will be a "No input file specified." error.

How you could do it:

server {
    listen   80;
    server_name  test.sc;
    root /usr/share/nginx/html;
    index index.html index.htm index.php;

    location /phpinfo {
        alias /usr/share/phppgadmin/;
        index phpinfo.php;

        location ~ \.php$ {
            fastcgi_pass unix:/var/run/php-fpm.sock;
            fastcgi_param SCRIPT_FILENAME $request_filename;
            include      fastcgi_params;
        }

    }

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include       fastcgi_params;
    }

}

This way, the php-files in the /usr/share/phppgadmin/ directory will be separately processed, you could even assign a different FPM-pool for those, in the context of /usr/share/phppgadmin/. Everything else will be processed using the defaults below. Nginx is limited to this behaviour because the first regex matching block will be used, it will not proceed to try any further or use multiple location blocks.

Now you can extend the configuration as you require:

server {
    listen   80;
    server_name  test.sc;
    root /usr/share/nginx/html;
    index index.html index.htm index.php;

    location /phpinfo {
        alias /usr/share/phppgadmin/;
        index phpinfo.php;

        location ~ \.php$ {
            fastcgi_pass unix:/var/run/php-fpm.sock;
            fastcgi_param SCRIPT_FILENAME $request_filename;
            include      fastcgi_params;
        }

    }

    location /phppgadmin {
        alias /usr/share/phppgadmin;
        index index.php;

        location ~ \.php$ {
            fastcgi_pass unix:/var/run/php-fpm.sock;
            fastcgi_param SCRIPT_FILENAME $request_filename;
            include  fastcgi_params;
        }
    }

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include       fastcgi_params;
    }

}

Additionally you can shorten this config by reusing certain parts:

/etc/nginx/php_settings:

location ~ \.php$ {
    fastcgi_pass unix:/var/run/php-fpm.sock;
    fastcgi_param SCRIPT_FILENAME $request_filename;
    include  fastcgi_params;
}

/etc/nginx/sites-enabled/000.conf:

server {
    listen   80;
    server_name  test.sc;
    root /usr/share/nginx/html;
    index index.html index.htm index.php;

    location /phpinfo {
        alias /usr/share/phppgadmin/;
        index phpinfo.php;

        include php_settings;
    }

    location /phppgadmin {
        alias /usr/share/phppgadmin;
        index index.php;

        include php_settings;
    }

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include       fastcgi_params;
    }

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.