Nurdin
Nurdin

Reputation: 23883

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

I got this error message when trying to set WordPress as a subdirectory. File not found. when view the URL example.com/blog

2020/05/05 20:55:56 [error] 906#906: *1 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: xxx.xxx.xxx.xxx, server: example.com, request: "GET /blog/ HTTP/1.1", upstream: "fastcgi://unix:/var/run/php/php7.2-fpm.sock:", host: "example.com"

example.conf

server {
    server_name example.com www.example.com;
    root /var/www/example/public;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    index index.html index.htm index.php;

    charset utf-8;

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

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

    error_page 404 /index.php;

    location /blog {
        alias /var/www/blog;
        index   index.html index.htm index.php;
        try_files $uri $uri/ /blog/index.php?$args;
    }

    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }
}

Upvotes: 1

Views: 4274

Answers (3)

tradenet
tradenet

Reputation: 11

This:

It has been suggested in a number of other "solutions" to this error that it has something to do with SCRIPT_FILENAME setting. I observed (eventually) that the parameter is set in a file, /etc/nginx/fastcgi.conf however in my config file, /etc/nginx/sites-available/x10.local.conf it has this directive: include fastcgi_params; Now there are both fastcgi_params and fastcgi_param files in the /etc/nginx/ directory. They appear to be identical except that the fastcgi_params file does not include the SCRIPT_FILENAME parameter. I added the line to the fastcgi_params, restarted php-fpm and nginx and found it finally worked.

Upvotes: 1

user1424074
user1424074

Reputation: 147

It has been suggested in a number of other "solutions" to this error that it has something to do with SCRIPT_FILENAME setting. I observed (eventually) that the parameter is set in a file, /etc/nginx/fastcgi.conf however in my config file, /etc/nginx/sites-available/x10.local.conf it has this directive: include fastcgi_params; Now there are both fastcgi_params and fastcgi_param files in the /etc/nginx/ directory. They appear to be identical except that the fastcgi_params file does not include the SCRIPT_FILENAME parameter. I added the line to the fastcgi_params, restarted php-fpm and nginx and found it finally worked.

Upvotes: 0

Malik Perang
Malik Perang

Reputation: 400

Please try this config instead

server {
    listen 80;
    server_name example.vm www.example.vm;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";
    index index.html index.htm index.php;

    charset utf-8;

    location / {
        root /var/www/laravel/public;
        try_files $uri $uri/ /index.php?$query_string;

        index index.html index.htm index.php;

        location ~ \.php$ {
            fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
            include fastcgi_params;
        }
    }

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

    error_page 404 /index.php;

    location /blog {
        root /var/www/;
        try_files $uri $uri/ /index.php?$args;

        location ~ \.php$ {
          fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
          fastcgi_index index.php;
          fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
          include fastcgi_params;
        }
    }



    location ~ /\.(?!well-known).* {
        deny all;
    }
}

Final updated answer tested and works. The issue here is the location path setting of alias/root directory. Maybe you can refer to this for more info in future. Configure nginx with multiple locations with different root folders on subdomain

Upvotes: 2

Related Questions