Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to pass request to php-fpm without nginx having volume mounted

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"

like image 794
You Care Avatar asked Aug 17 '18 17:08

You Care


People also ask

Does nginx require PHP-FPM?

Nginx is the DevOps community's most beloved http web server. And developers love the PHP programming language because it enables them to quickly build and deploy interactive websites. As such, it's no wonder that so many sys admins need to configure Nginx, PHP and PHP-FPM on both Linux and Windows servers.

Does nginx need PHP?

Since Nginx does not contain native PHP processing like some other web servers, we will need to install php-fpm , which stands for “fastCGI process manager”. We will tell Nginx to pass PHP requests to this software for processing.

What is PHP-FPM Nginx?

PHP-FPM (FastCGI Process Manager) is an alternative to FastCGI implementation of PHP with some additional features useful for sites with high traffic. It is the preferred method of processing PHP pages with NGINX and is faster than traditional CGI based methods such as SUPHP or mod_php for running a PHP script.

What user does PHP-FPM run as?

By default the web server and php-fpm runs with the user called www-data.


1 Answers

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;
}
like image 107
Richard Smith Avatar answered Sep 27 '22 17:09

Richard Smith