I have set up a docker container with PHP + NGINX. I made a change to the NGINX config so that it redirects to index.php
in the root folder if it does not find any file or directory. It started looping adding /index.php/
to the URL infinitely. I changed the config back to:
server {
listen 0.0.0.0:80;
root /var/www/html;
location / {
index index.php index.html;
}
location ~ .php$ {
include fastcgi_params;
fastcgi_pass php:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
}
}
And now it only redirects once, to http://localhost/index.php/.
I don’t think its my poorly built PHP routers fault as when I remove that code it continues to redirect. Here is the routers code from index.php
anyway:
<?php
$reqParsed = parse_url($SERVER['REQUESTURI']);
$viewsDir = '/static/views/';
$manifest = jsondecode(fileget_contents(__DIR . '/v1/manifest.json'), true);
switch ($reqParsed['path']) {
case '':
case '/':
require __DIR . $viewsDir . 'home.php';
break;
case '/posts/':
if (isset($_GET['id'])) {
if (isset($manifest['posts'][$GET['id']])) {
require DIR . $viewsDir . 'posts/post-view.php';
}else {
require DIR . $viewsDir . 'posts/post-404.php';
}
}else{
require _DIR . $viewsDir . 'posts/posts.php';
}
echo $GET['id'];
break;
default:
httpresponse_code(404);
require __DIR . $viewsDir . '404.php';
}
?>
Thanks for any help in advance!
2
Answers
I have just checked in another browser than Firefox (What I should have done in the first place.), and on Firefox on another pc, It does not append /index.php/ on there. I dont know why it does append this on my laptops Firefox.
Thanks for everyone's help and sorry for wasting your time.
Since you think that the problem is with the NGINX configuration I’ll focus on that and use the following simplified PHP file:
My project layout is:
Here’s the NGINX configuration:
The
Dockerfile
anddocker-compose.yml
:Go to http://127.0.0.1:8000.
I suggest that you start with this setup, verify that it works and then insert your
index.php
in place of my simple one.