【问题标题】:Set up docker-compose with nginx missing snippets/fastcgi-php.conf使用 nginx 缺少片段/fastcgi-php.conf 设置 docker-compose
【发布时间】:2019-07-20 09:01:38
【问题描述】:

我正在尝试设置一个新的 docker-compose 文件。

version: '3'
services:

  webserver:
      image: nginx:latest
      container_name: redux-webserver
      # working_dir: /application
      volumes:
        - ./www:/var/www/
        - ./docker/nginx/site.conf:/etc/nginx/conf.d/default.conf
      ports:
        - "7007:80"

目前它非常简单。但是我复制了以下配置:

# Default server configuration
#
server {
   listen 7007 default_server;
   listen [::]:7007 default_server;


   root /var/www;

   # Add index.php to the list if you are using PHP
   index index.html index.htm index.nginx-debian.html;

   server_name example;

   location / {
      # First attempt to serve request as file, then
      # as directory, then fall back to displaying a 404.
      try_files $uri $uri/=404;
   }

    location /redux {
        alias /var/www/Redux/src;
        try_files $uri @redux;

        location ~ \.php$ {
            include snippets/fastcgi-php.conf;
            fastcgi_param SCRIPT_FILENAME $request_filename;
            fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
        }
    }

    location @redux {
        rewrite /redux/(.*)$ /redux/index.php?/$1 last;
    }

   # pass PHP scripts to FastCGI server
   #
   location ~ \.php$ {
      include snippets/fastcgi-php.conf;

        #fastcgi_split_path_info ^(.+\.php)(/.+)$;

      # With php-fpm (or other unix sockets):
      fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
        #fastcgi_index index.php;
      # With php-cgi (or other tcp sockets):
      # fastcgi_pass 127.0.0.1:9000;
   }

   # deny access to .htaccess files, if Apache's document root
   # concurs with nginx's one
   #
   location ~ /\.ht {
      deny all;
   }
}

但现在当我尝试使用 docker-compose run webserver 启动它时,我收到以下错误:

2019/07/20 08:55:09 [emerg] 1#1: open() "/etc/nginx/snippets/fastcgi-php.conf" failed (2: No such file or directory) in /etc/nginx/conf.d/default.conf:59
nginx: [emerg] open() "/etc/nginx/snippets/fastcgi-php.conf" failed (2: No such file or directory) in /etc/nginx/conf.d/default.conf:59

我知道它没有找到文件 fastcgi-php.conf。但这是为什么呢?该文件不应该包含在标准 nginx 安装中吗?

【问题讨论】:

    标签: docker nginx docker-compose


    【解决方案1】:

    /etc/nginx/snippets/fastcgi-php.confnginx-full包中,但是你使用的镜像nginx:latest没有安装nginx-full包。

    要拥有它,您需要从 nginx:latest 编写自己的 dockerfile 库并在其中安装 nginx-full

    Dockerfile:

    FROM nginx:latest
    RUN apt-get update && apt-get install -y nginx-full
    

    docker-compose.yaml:

    version: '3'
    services:
      webserver:
        build: .
        image: mynginx:latest
    

    Dockerfiledocker-compose.yaml 放在同一个文件夹中然后向上。

    另外,如果你不介意使用其他人的 repo(意味着不是官方的),你可以从 dockerhub 中搜索一个,例如我从 dockerhub (schleyk/nginx-full) 找到的一个:

    docker run -it --rm schleyk/nginx-full ls -alh /etc/nginx/snippets/fastcgi-php.conf
    -rw-r--r-- 1 root root 422 Apr  6  2018 /etc/nginx/snippets/fastcgi-php.conf
    

    【讨论】:

    • 是否可以在docker-compose文件中安装附加包?
    • 不,这是 Dockerfile 的设计选择,docker-compose 不是为了这个目的,因为 compose 文件中的 build . 可以在 docker-compose up -ddocker-compose up -d --build 时为您构建,所以把它放在 Dockerfile 中是最好的做法,我猜不会为你增加任何努力。
    • 或者,你可以把包安装到dockerfile中,打标签,把你自己的镜像放到dockerhub,然后你的docker-compose就可以直接使用你的镜像,不需要定义build: .,这样就可以了将对所有成员透明。
    • hmm 好吧,买一个已经包含 nginx、php7.3 和 mysql 的可能是个好主意
    • docker hub 上有很多,但我不得不说这不符合 docker 的最佳实践。 Docker 的规则是一个服务一个容器,参见this,也是一个将 db & webapp 分开到不同容器的示例:docs.docker.com/compose/django 但是,如果你坚持,docker 也可以为你做,参考docs.docker.com/config/containers/multi-service_container
    【解决方案2】:

    您正在尝试使用 docker compose 配置,该配置不考虑您尝试加载 fastcgi / php 特定选项。

    您可以使用另一个图像并将其链接到您的网络服务器,例如:

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

    来源,有更详尽的解释:http://geekyplatypus.com/dockerise-your-php-application-with-nginx-and-php7-fpm/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-19
      • 1970-01-01
      • 2021-10-21
      • 1970-01-01
      • 1970-01-01
      • 2017-08-20
      相关资源
      最近更新 更多