0

Wit the following setup I don't know how to make nginx pass the CGI request to the index script in the appropriate version subdirectory. It instead serves the index as a download.

PHP-FPM is a linked docker container.

server {
    listen 80;
    root /var/www/html/public;
    server_name _;
    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    index index.html index.htm index.debian-default.html index.php;

    location ~ /(?<version>v.*)/ {
        try_files $uri $uri/ /$version/index.php$is_args$args;
    }

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass php-fpm:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}

If I nest the proxy location block and change script filename into:

fastcgi_param SCRIPT_FILENAME $document_root/$version/$fastcgi_script_name;

when querying http://localhost/v1/ I get

nginx_1       | 2021/05/19 07:19:28 [error] 29#29: *3 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 172.26.0.1, server: _, request: "GET /v1/ HTTP/1.1", upstream: "fastcgi://172.26.0.2:9000", host: "localhost:9081"
nginx_1       | 172.26.0.1 - - [19/May/2021:07:19:28 +0000] "GET /v1/ HTTP/1.1" 404 27 "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:88.0) Gecko/20100101 Firefox/88.0"
php-fpm_1     | 172.26.0.3 -  19/May/2021:07:19:28 +0000 "GET /v1/index.php" 404

in the nginx and php-fpm logs respectively.

3
  • What is the php-fpm address?
    – Paul
    May 18, 2021 at 20:35
  • 1
    @Paul it's the linked docker container. It's perfectly ok. May 19, 2021 at 7:31
  • and 172.26.0.2:900 can be accessed?
    – djdomi
    Jun 16, 2021 at 18:17

2 Answers 2

0

Using the hint in this answer, I changed the nginx config to

location ~ /(?<version>v.*)/ {
    try_files $uri $uri/ /$version/index.php$is_args$args;

    location ~ ^(.*\.php)(.*)$ {
        ...
        fastcgi_param SCRIPT_FILENAME $document_root$1;
        ...
    }
}

and it started to work as expected.

-1

Your fastcgi_param should be:

fastcgi_pass 127.0.0.1:9000;

if you're running php-fpm locally. If you're running php-fpm as a socket:

fastcgi_pass unix:/var/run/php/php7.3-fpm.sock;

be sure to replace php7.3 part with your php version.

1
  • No. php-fpmis a valid hostname. This is not an issue. The issue is with $version parameter from first location block and how to pass it to the proxy block. May 19, 2021 at 6:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .