7
votes

I'm trying to migrate my legacy monolith to k8s, now I have nginx and php-fpm (with code) images and I want nginx to just serve http traffic and pass it to fpm, but nginx insist on having files, I don't have try_files directive, but it tries to find root and index files anyways.

So is it at all possible to not mount source code to nginx, I really don't see why it should be there, but I couldn't find any working example

nginx.conf:

server {
    listen 80;

    index index.php;
    # This dir exist only in php-fpm container
    root /var/www/html/public; 

    location ~* \.php$ {

        client_max_body_size 0;

        include fastcgi_params;
        fastcgi_pass php-fpm:9000;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;
    }
}

2018/08/17 16:44:40 [error] 9#9: *46 "/var/www/html/public/index.php" is not found (2: No such file or directory), client: 192.xxx.xxx.xxx, server: , request: "GET / HTTP/1.1", host: "localhost"

192.xxx.xxx.xxx - - [17/Aug/2018:16:44:40 +0000] "GET / HTTP/1.1" 404 571 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.146 Safari/537.36" "195.xxx.xxx.xxx"

1
I wonder if $realpath_root is the correct variable to use if the path does not exist for nginx. You could try using $document_root instead. - Richard Smith
@RichardSmith idk I tried: fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param DOCUMENT_ROOT $document_root; didn't help - You Care

1 Answers

9
votes

The problem is that the index directive needs the file index.php to exist, in order to internally redirect the URI / to /index.php.

You can avoid the index directive by adding a location / to internally redirect everything to /index.php.

For example:

location / {
    rewrite ^ /index.php last;
}
location ~* \.php$ {
    root /var/www/html/public; 
    client_max_body_size 0;

    include fastcgi_params;
    fastcgi_pass php-fpm:9000;
    fastcgi_split_path_info ^(.+\.php)(/.*)$;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param DOCUMENT_ROOT $document_root;
}