Score:1

Handling Nginx location aliases when using a standalone PHP-FPM server

bd flag

I host various Laravel applications that use various versions of PHP, and decided to improve the current single bloated container Docker setup by having multiple PHP-FPM containers, one for each PHP version.

I tested the setup by having a PHP file at /var/www/html/index.php mounted to all containers and it works as expected.

However, since Laravel apps are served from a public subdirectory, this causes issue when the PHP container tries to find the file.

This is the Nginx config:

location /appname/ {
        alias /var/www/html/appname/public;
        index index.php;

        # Use PHP 7
                location ~ \.php$ {
                        fastcgi_pass php74:9007;
                        fastcgi_index index.php;
                        include fastcgi_params;
                        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
                }
    }

So a request to /appname/index.php actually gets served from /var/www/html/appname/public, but the PHP containers don't know this and can't find the file.

I tried replacing $document_root with /var/www/html/appname/public or /appname/public but neither worked.

This is the error PHP-FPM is throwing:

FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream

How do I go about letting PHP know the path to properly serve the app?

Feel free to recommend a better container setup to achieve this goal.

Score:2
jp flag

Both the location and alias values should have a trailing / for correct operation.

When using an alias directive, use:

fastcgi_param  SCRIPT_FILENAME $request_filename;
slightly_toasted avatar
bd flag
Solved. Thank you!
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.