Nginx – error 200#0: *79 FastCGI sent in stderr: “Primary script unknown” while reading response header from upstream,

mac-osxnginxPHP

I want to configure new nginx on my MAC but I got always get [error] 200#0: *79 FastCGI sent in stderr: "Primary script unknown".. I don't know what Im doing wrong with config. This is my nginx.conf file:

#user  RobDee;
worker_processes  auto;

#error_log  logs/error.log;
#error_log  logs/error.log  info;
pid        logs/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    #log_format  #main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

     access_log  logs/access.log;
     error_log  logs/error.log;
    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;


    server {
        listen       80;
        server_name  local.mydomain.co.uk local.beer.telegraph.co.uk;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   /Users/RobDee/workspace/beer/web;
            index  index.html index.htm;
        }

        #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   html;
        }

        # proxy the PHP scripts to Apache listening on 127.0.0.1:80
        #
        #location ~ \.php$ {
        #    proxy_pass   http://127.0.0.1;
        #}

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

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


    # another virtual host using mix of IP-, name-, and port-based configuration
    #
    #server {
    #    listen       8000;
    #    listen       somename:8080;
    #    server_name  somename  alias  another.alias;

    #    location / {
    #        root   html;
    #        index  index.html index.htm;
    #    }
    #}


    # HTTPS server


   server {
    listen                     443 ;
    server_name                local.mydomain.co.uk local.beer.telegraph.co.uk;

    ssl                        on;
    ssl_protocols              TLSv1 TLSv1.1 TLSv1.2;
    ssl_certificate            /usr/local/etc/nginx/cert.pem;
    ssl_certificate_key        /usr/local/etc/nginx/cert.key;

    gzip_disable "msie6";
    gzip_types text/plain application/xml application/x-javascript text/css application/json text/javascript;

    access_log  /usr/local/var/log/nginx/access.log;
    error_log   /usr/local/var/log/nginx/error.log;
    log_not_found off;
    root    /Users/RobDee/workspace/beer/web;

    location /.htpasswd
    {
        return 403;
    }
    location ~ \.css {
        root /Users/RobDee/workspace/beer/web;
        expires max;
    }

    location ~* \.(jpg|jpeg|png|gif|ico|js|woff|woff2|ttf)$ {
        root /Users/RobDee/workspace/beer/web;
        access_log off;
        expires max;
    }

    location ~* \.(js|css)$ {
        expires 1y;
        log_not_found off;
    }

    location /
    {
        root /Users/RobDee/workspace/beer/web;
        try_files $uri $uri/ /app_dev.php$is_args$args;
         index app_dev.php;
    }

    location ~ \.php$ {
        root /Users/RobDee/workspace/beer/web;
    fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  /Users/RobDee/workspace/beer/web/app_dev.php;
        fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
        include        fastcgi_params;
        fastcgi_read_timeout 3000;

    }
    }

    include servers/*;

    }

Maybe someone will see some errors in my conf.

Best Answer

"Primary script unknown" almost always means that the value for SCRIPT_FILENAME is wrong.

The problem is likely these three lines:

root /Users/RobDee/workspace/beer/web;
fastcgi_param  SCRIPT_FILENAME  /scripts$fastcgi_script_name;
include        fastcgi_params;

You set the $document_root, then hardwire it as /scripts. Which is it?

In a normal nginx server block, you would set the root once near the top of the server block and allow all (or most) location blocks to inherit the same value. Only override where necessary. See this document for details.

The usual format for SCRIPT_FILENAME is either of these:

fastcgi_param   SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param   SCRIPT_FILENAME $request_filename;

Which in your configuration would equate to the same value. Notice that $document_root is defined by the value of the root directive.

Finally, including fastcgi_params last may or may not be ok. In some versions of fastcgi_params there may be conflicting definitions for fastcgi_param statements you have explicitly defined. You should always include the file before explicit fastcgi_param statements.

For example:

server {
    ...
    root /Users/RobDee/workspace/beer/web;
    ...
    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        include        fastcgi_params;
        fastcgi_param  SCRIPT_FILENAME  $request_filename;
        fastcgi_read_timeout 3000;
    }
}

The fastcgi_index directive has no meaning in this context.