是否可以在 nginx 没有安装卷的情况下将请求传递给 php-fpm

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

我正在尝试将我的遗留单体迁移到 k8s,现在我有 nginx 和 php-fpm(带代码)图像,我希望 nginx 只提供 http 流量并将其传递给 fpm,但是nginx 坚持要有文件,我没有 try_files 指令,但它无论如何都会尝试查找根文件和索引文件。

那么有没有可能不将源代码挂载到 nginx,我真的不明白为什么它应该在那里,但我找不到任何工作示例

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"

问题是 index 指令需要文件 index.php 存在,以便在内部将 URI / 重定向到 /index.php

您可以通过添加 location / 来在内部将所有内容重定向到 /index.php 来避免 index 指令。

例如:

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;
}