skip to Main Content

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


  1. Chosen as BEST ANSWER

    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.


  2. Since you think that the problem is with the NGINX configuration I’ll focus on that and use the following simplified PHP file:

    <?php
    phpinfo();
    ?>
    

    My project layout is:

    ├── default.conf
    ├── docker-compose.yml
    ├── Dockerfile
    └── web
        └── index.php
    

    Here’s the NGINX configuration:

    server {
        listen 80;
        server_name localhost;
    
        root /usr/share/nginx/html;
        index index.php index.html index.htm;
    
        location / {
            try_files $uri $uri/ /index.php?$query_string;
        }
    
        location ~ .php$ {
            include fastcgi_params;
            fastcgi_pass php:9000;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        }
    }
    

    The Dockerfile and docker-compose.yml:

    FROM php:7.2.1-fpm
    
    WORKDIR /usr/share/nginx/html
    
    EXPOSE 9000
    
    version: '3.7'
    
    services:
      php:
        container_name: php
        build:
          context: .
          dockerfile: Dockerfile
        volumes:
          - ./web:/usr/share/nginx/html
    
      nginx:
        image: nginx:latest
        container_name: nginx
        ports:
          - "8000:80"
        volumes:
          - ./web:/usr/share/nginx/html
          - ./default.conf:/etc/nginx/conf.d/default.conf
        depends_on:
          - php
    

    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.

    enter image description here

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search