docker-library/drupal

FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream

zar3bski opened this issue · 4 comments

Am I the only one struggling using drupal:8.8.1-fpm-alpine with Nginx? Either I missed an obvious part about how FPM works or there is some ENV VARs to be tuned with this image:

upstream drupal_user_container {
        server user_asmodius_drupal:9000;
    }

server {
    listen 80;
    server_name user.eu; 
    root /var/www/html/user;

        location = /favicon.ico {
        log_not_found off;
        access_log off;
    }

    location = /robots.txt {
        allow all;
        log_not_found off;
        access_log off;
    }

    location ~ ^/sites/.*/private/ {
        return 403;
    }

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

    location @rewrite {
        rewrite ^/(.*)$ /index.php?q=$1;
    }

    location ~ /vendor/.*\.php$ {
        deny all;
        return 404;
    }

    location ~ '\.php$|^/update.php' {
        fastcgi_split_path_info ^(.+?\.php)(|/.*)$;
        include fastcgi_params;
        fastcgi_param HTTP_PROXY "";
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name; # HERE IS PROBABLY THE ISSUE
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_intercept_errors on;

        fastcgi_pass drupal_user_container; 
    }

    location ~ ^/sites/.*/files/styles/ { # For Drupal >= 7
        try_files $uri @rewrite;
    }

    # Handle private files through Drupal.
    location ~ ^/system/files/ { # For Drupal >= 7
        try_files $uri /index.php?$query_string;
    }

    # prevent hotlinking
    location ~ ^/sites/.*/files/ {
        if ($invalid_referer) {
          return 403;
        }
    }

    location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
        if ($invalid_referer) {
          return 403;
        }
        expires max;
        log_not_found off;
    }

Nothing quite fancy with this docker compose, I just mount /var/www/html so that Nginx could serve static files

  nginx: 
    container_name: ${SERVER_NAME}_nginx
    restart: always
    networks: 
      - monitoring
      - web
    image: nginx:1.17.5-alpine
    entrypoint: /entrypoint.sh
    ports: 
      - 80:80
      - 443:443
    volumes: 
      - user_www:/var/www/html/user:ro

  drupal:
    restart: always
    container_name: "user_${SERVER_NAME}_drupal"
    image: drupal:8.8.1-fpm-alpine
    networks:
      - web
      - backend
    volumes:
      - user_www:/var/www/html

Here is the error:

 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 192.168.128.1, server: user.eu, request: "GET / HTTP/1.1", upstream: "fastcgi://192.168.144.4:9000", host: "user.eu"

https://serverfault.com/a/517327

The error message “primary script unknown” is almost always related to a wrongly set SCRIPT_FILENAME in the nginx fastcgi_param directive (or incorrect permissions, see other answers).

Yeah I came to the conclusion that it came from SCRIPT_FILENAME. My question was, rather, how to find this name for this specific image (I am a pure noob on PHP)?

I'd suggest checking https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/#fastcgi-path-in-script-filename and https://serverfault.com/a/496031/58240 or https://serverfault.com/a/922596/58240 which recommends using $request_filename for the value of SCRIPT_FILENAME.

https://www.nginx.com/resources/wiki/start/topics/recipes/drupal/ might also be useful, but it looks like that might've already been a source of inspiration for your current configuration?

If all else fails, you'll probably have a lot more luck with the apache variant, which can be used via proxy_pass (and thus is much easier to get working correctly).

It seems like fpm receives the full path from nginx and tries to find the files in the fpm container, so it must be the exactly the same as server.root in the nginx config, even if it doesn't exist in the nginx container.
source: https://stackoverflow.com/questions/29905953/how-to-correctly-link-php-fpm-and-nginx-docker-containers