0

I`m trying to set up postfixadmin nginx config file with nested locations and stuck with problem of passing correct SCRIPT_FILENAME to php-fpm backend.

Urls that should be passed as postfixadmin scripts starts with /postfixadmin URL. Part of my config file that serve /postfixadmin files look like this

location ~ ^\/postfixadmin.*(\.php|\.css|\.png|\.gif) {

            location ~ (.*\.php)$ {

                    alias /var/www/postfixadmin/public/;

                    fastcgi_param REQUEST_METHOD $request_method;
                    fastcgi_param SCRIPT_FILENAME $document_root$1;
                    fastcgi_param CONTENT_TYPE    $content_type;
                    fastcgi_param CONTENT_LENGTH  $content_length;
                    fastcgi_param QUERY_STRING    $query_string;

                    fastcgi_pass  127.0.0.1:9000;
                    proxy_read_timeout 500;
            }
       }

And problem in this location

location ~ (.*\.php)$ {

What i want to do is to serve all usrl related to /postfixadmin and with extension ".php" as php scripts and catch up script name as $1 variable. But in fact when I`m trying to access URL /postfixadmin/index.php I get the following header

HTTP/1.1 404 Not Found
Server: nginx/1.16.1
Date: Sun, 19 Jan 2020 10:56:13 GMT
Content-Type: text/html; charset=UTF-8
Connection: keep-alive
X-Powered-By: PHP/7.4.1

So as you can see nginx properly choose location

location ~ ^\/postfixadmin.*(\.php|\.css|\.png|\.gif) {

And then nginx sees .php in the end of URL and trying to send this to php-fpm backend. But php-fpm answers with 404 status code. And this happens because of empty SCRIPT_FILENAME.

Maybe I`m wrong but I think that problem in this location

location ~ (.*\.php)$ {

Because when I make it looks like this, all works fine and I can pass $1 variable (as name of script) to php-fpm backend.

location ~ .*\/(.*\.php)$ {

But specify .*/ before location make no sense (because I want to specify just extension with script name) and I`m just try to understand how to do this right.

Thanks

Meksvinz
  • 1
  • 1
  • See [this answer](https://stackoverflow.com/questions/42443468/nginx-location-configuration-subfolders/42467562#42467562). – Richard Smith Jan 19 '20 at 11:49

1 Answers1

0

Ok. I figured out cause of problem. Problem was in my dummy regex.

So i fixed this

location ~ (.*\.php)$ {

To this

location ~ ([^\/]*\.php)$ {

So I just exclude "/" symbol from pattern.

The old type of regex include everything before .php plus whole URL.

The new one include everything after last "/"

Thats it!

Meksvinz
  • 1
  • 1