Docker + nginx + PHP - nginx不指向根目录?

时间:2017-01-01 20:02:36

标签: php nginx docker docker-compose

我按照这个guide在Docker上运行PHP,但是nginx 指向我在site.conf中设置的根目录。

搬运工-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;
    }
}

代码/ index.php的:

<?php
echo phpinfo();

在我的终端上,我跑:

docker-compose up

结果:

enter image description here

但它应该是:

enter image description here

任何想法为什么?

修改

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

运行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"

docker.php来自哪里?

1 个答案:

答案 0 :(得分:2)

您有域名冲突。在site.conf中设置server_name docker.loc;。然后将127.0.0.1 docker.loc添加到本地计算机上的/ etc / hosts。然后,您可以通过docker.loc:8080

访问dockerised应用程序

或者您可以尝试解决冲突。我认为问题可能在/etc/nginx/sites-available/default