Run
Run

Reputation: 57286

Docker + nginx + PHP - nginx does not point to the root directory?

I follow this guide to run PHP on Docker, but nginx does not point to the root directory that I have set in site.conf.

docker-compose.yml:

nginx:
    image: nginx:latest
    ports:
        - 8080:80
    volumes:
        - ./code:/code
        - ./site.conf:/etc/nginx/conf.d/site.conf
    links:
        - php
php:
    image: php:7-fpm
    volumes:
        - ./code:/code

site.conf:

server {
    index index.php index.html;
    server_name localhost;
    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root /code;

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass php:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}

code/index.php:

<?php
echo phpinfo();

On my terminal, I run:

docker-compose up

Result:

enter image description here

But it should be:

enter image description here

Any ideas why?

EDIT:

server {
    index index.php index.html;
    server_name docker.loc;
    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root /code;

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass php:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}

I get this error when I run docker-compose up:

nginx_1 | 172.17.0.1 - - [01/Jan/2017:20:35:17 +0000] "GET / HTTP/1.1" 403 571 "-" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/55.0.2883.87 Chrome/55.0.2883.87 Safari/537.36"
nginx_1 | 2017/01/01 20:35:17 [error] 6#6: *5 directory index of "/code/" is forbidden, client: 172.17.0.1, server: docker.php, request: "GET / HTTP/1.1", host: "docker.php:8080"

Where is docker.php coming from??

Upvotes: 2

Views: 2212

Answers (1)

Artem Ilchenko
Artem Ilchenko

Reputation: 1035

You have domain name conflict. Set server_name docker.loc; in site.conf. And then add 127.0.0.1 docker.loc to /etc/hosts on your local machine. Then you can get access to dockerised application by docker.loc:8080

Or you can try resolve conflict. I think problem maybe in /etc/nginx/sites-available/default

Upvotes: 2

Related Questions