nginx&在docker中的php-fpm:不能finx index.php

时间:2018-04-06 15:01:47

标签: php docker nginx

尝试nginx php-fpm通过fastcgi docker撰写php-fpm"root /var/www/code"容器git将容器内的代码拉入/ var / www / code /。 当我从nginx's site.conf文件中注释掉/etc/nginx/html/index.php指令时,它会调查web_1 | 2018/04/06 14:59:04 [error] 7#7: *1 "/etc/nginx/html/index.php" is not found (2: No such file or directory), client: 172.19.0.1, server: localhost, request: "GET / HTTP/1.1", host: "0.0.0.0:8080" web_1 | 172.19.0.1 - - [06/Apr/2018:14:59:04 +0000] "GET / HTTP/1.1" 404 169 "-" "curl/7.47.0"

nginx

这是预期的行为吗?我是否还需要将应用的代码放在var/www/code的{​​{1}}容器中?是不是只能将php-fpm容器用于此目的,而不必将应用程序的代码也存储在nginx容器中?

nginx的site.conf:

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

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

来自主持人:

# curl 0.0.0.0:8080
<html>
<head><title>404 Not Found</title></head>
<body bgcolor="white">
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.13.9</center>
</body>
</html>

来自docker日志web_1:

web_1         | 2018/04/06 14:54:37 [error] 7#7: *5 "/var/www/code/public/index.php" is not found (2: No such file or directory), client: 172.19.0.1, server: localhost, request: "GET / HTTP/1.1", host: "0.0.0.0:8080"
web_1         | 172.19.0.1 - - [06/Apr/2018:14:54:37 +0000] "GET / HTTP/1.1" 404 169 "-" "curl/7.47.0"

搬运工-comnpose.yaml:

version: '2'

services:
    web:
        build:
          context: ./web
        ports:
            - "8080:8000"
        volumes:
            - ./web/site.conf:/etc/nginx/conf.d/site.conf
        networks:
            - redis-cluster
    php-worker:
      build:
        context: ./php-worker
      ports:
            - 9000:9000
      volumes:
            - ./php-worker/www.conf:/usr/local/etc/php-fpm.d/www.conf
      networks:
        redis-cluster:
#          ipv4_address: 172.18.0.10
    db:
      build:
        context: ./db
networks:
    redis-cluster:
      ipam:
        driver: host
        config:
          - subnet: 172.18.0.0/24

2 个答案:

答案 0 :(得分:0)

你的nginx容器中的md create / var / www / code文件夹? 但我认为将代理全部代理到php是个坏主意。更好地使用nginx用于所有静态,动态用于php-fpm

答案 1 :(得分:0)

这是在我的情况下工作的nginx配置:

server {
    listen       8000 default_server;
    server_name  localhost;
    root   /var/www/code;

    #charset koi8-r;
    #access_log  /var/log/nginx/host.access.log  main;

    location / {
        index  index.php;
        try_files $uri $uri/ /index.php?$args;
    }

    #error_page  404              /404.html;

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }


    # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
    #
    location ~ \.php$ {
        fastcgi_pass   php-worker:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include        fastcgi_params;
    }
}