如何使用PHP-FPM和Nginx Docker容器解析未知的PHP-FPM主脚本?

时间:2016-12-14 19:04:19

标签: php nginx docker

我的情况是这样,我有两个Docker容器:

  1. 在端口9000上运行PHP-FPM
  2. 运行nginx并拥有PHP文件(PHP-FPM容器是否可以访问这些文件?)
  3. 我一直收到以下错误:

    FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 172.17.0.1, ser
    ver: _, request: "GET / HTTP/1.1", upstream: "fastcgi://172.17.0.2:9000", host: "172.17.0.3"
    

    我读到here这是“始终与nginx SCRIPT_FILENAME指令中的错误设置fastcgi_param相关。”

    问题是,我不知道如何解决它:-P

    在容器2中配置:

    server {
        listen 80 default_server;
        listen [::]:80 default_server;
    
        charset UTF-8;
        root /var/www/WordPress;
    
        index index.php index.html index.htm;
        server_name _;
        location / {
            try_files $uri/ /index.php?$args;
        }
        location ~ \.php$ {
            try_files $uri =404;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_index index.php;
            fastcgi_pass 172.17.0.2:9000;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME /var/www/WordPress$fastcgi_script_name;
            # set headers
            add_header Cache-Control $cc;
            access_log off;
            expires $ex;
        }
        location ~* \.(js|css|svg|png|jpg|jpeg|gif|ico|eot|otf|ttf|woff)$ {
            add_header Access-Control-Allow-Origin *;
            add_header Cache-Control "public";
            access_log off;
            log_not_found off;
            expires 1y;
        }
        location ~ /\.ht {
               deny all;
        }
    }
    

3 个答案:

答案 0 :(得分:0)

我没有nginx专家,但实际上the docs fastcgi_param SCRIPT_FILENAME中的每个实例都以$fastcgi_script_name结尾,而你自己并没有。

答案 1 :(得分:0)

将根线更改为:root /var/www/WordPress/;$fastcgi_script_name不包括/

答案 2 :(得分:0)

我不知所措,试图弄清为什么它不起作用。 和往常一样,这是一个注意力不集中的问题。 因此,在我的nginx.conf中,有以下几行:

set $root "/var/www/html/web";

root   $root;
set $bootstrap "index.php";
index  $bootstrap;

location / {
    index  index.html $bootstrap;
    try_files $uri $uri/ /$bootstrap?$args;
    expires    -1;
}

location ~ \.php$ {
    fastcgi_split_path_info  ^(.+.php)(.*)$;

    set $fsn /$bootstrap;
    if (-f $document_root$fastcgi_script_name) {
        set $fsn $fastcgi_script_name;
    }

    fastcgi_pass php:9000;
    fastcgi_index $bootstrap;
    include fastcgi_params;

    fastcgi_param  SCRIPT_FILENAME  $document_root$fsn;

    fastcgi_param  PATH_INFO        $fastcgi_path_info;
    fastcgi_param  PATH_TRANSLATED  $document_root$fsn;

    try_files $fsn =404;
    expires    -1;
}

include defaults.conf;

注意if (-f $document_root$fastcgi_script_name) {行非常重要,因为这是对存在性的检查。 在我的docker-compose.yml中,我在nginx容器和php容器中安装了不同的支架:

php:
  image: aspendigital/octobercms:php7.2-fpm
  container_name: "${PROJECT_NAME}_php"
  volumes:
    - ./web:/var/www/html # <- this line is incorrect! Mounts MUST be identical! 
nginx:
  image: wodby/nginx:$NGINX_TAG
  container_name: "${PROJECT_NAME}_nginx"
  depends_on:
    - php
  environment:
    NGINX_STATIC_OPEN_FILE_CACHE: "off"
    NGINX_ERROR_LOG_LEVEL: debug
    NGINX_BACKEND_HOST: php
    NGINX_SERVER_ROOT: /var/www/html/web/
    NGINX_CONF_INCLUDE: /var/www/html/nginx/*.conf
  volumes:
    - ./:/var/www/html:cached

因此,将php-part中的卷替换为与nginx-part中相同的卷可以解决我的问题。 小心!

相关问题