John Davidson

nginx - php-fpm: File not found in kubernetes

0 comments
Message:


I'm trying to set a k8s deployment with 2 pods (nginx + php-fpm), but I can't get php-fpm to execute the php scripts. The webpage displays File not found., and in logs :



  • fpm pod : 192.168.3.187 - 13/Nov/2020:16:44:06 +0000 "GET /index.php" 404

  • nginx pod : 2020/11/13 16:44:06 [error] 20#20: *1 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 192.168.1.45, server: _, request: "GET / HTTP/1.1", upstream: "fastcgi://10.98.178.35:9000", host: "192.168.1.220"


The .html files are served as expected.


Without the Location ~ \.php$ server block of nginx I'm able to download .php files, so the problem is really with php-fpm; when entering the container, I can see the scripts are present.


In my deployment, a PersistentVolume on an NFS is mounted on both pods, in /app. Nginx is configured with this configMap :


  default.conf: |
server {
listen 80 default_server;
server_name _;

add_header X-Backend-Server $hostname;

root /app;
index index.php index.html;

location / {
try_files $uri $uri/ =404;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_param REQUEST_METHOD $request_method;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass phpfpm-deploy-svc.default.svc.cluster.local:9000;
}
}

Where phpfpm-deploy-svc.default.svc.cluster.local is the DNS resolution of the PHP-FPM service (irrelevant to paste the service code).


php-fpm is a classic deployment :


spec:
containers:
- name: phpfpm
image: php:7.3-fpm
ports:
- containerPort: 9000
volumeMounts:
- name: nfs-volume
mountPath: /app
volumes:
- name: nfs-volume
persistentVolumeClaim:
claimName: nfs-pvc

with its own service :


apiVersion: v1
kind: Service
metadata:
name: phpfpm-deploy-svc
spec:
ports:
- port: 9000
selector:
app: phpfpm-deploy
type: ClusterIP

Most of the solutions seen here are related to the line fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;: that's possible, but I still has to find a working one.


Without k8s (only docker-compose), I can create this configuration witout issue.



My Answer:
One possible solution to this issue could be to check the permissions of the PHP files within the NFS volume. Make sure that the PHP files are readable by the user running the PHP-FPM process inside the container.

You can also try to modify the SCRIPT_FILENAME parameter in the nginx configuration to include the full path to the PHP files within the NFS volume. For example, you can try changing it to:


fastcgi_param SCRIPT_FILENAME /app$fastcgi_script_name;


Additionally, you can try to add the following line to the location block for PHP files in the nginx configuration:


fastcgi_split_path_info ^(.+\.php)(/.+)$;


This line helps to properly split the request URI into the PHP script file and any additional path info.

After making these changes, restart both the nginx and PHP-FPM pods to apply the new configuration. This should hopefully resolve the "File not found" issue with PHP scripts in your Kubernetes deployment.

Rate this post

4 of 5 based on 5153 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us