Henrique Gonçalves
Henrique Gonçalves

Reputation: 1582

FastCGI sent in stderr: "Primary script unknown" - nginx and silex

I'm getting this error on a server where I've deployed a Silex project.

In this server I already had nginx passing requests from / to a Flask app running in port 5000.

So, after pulling the new project from my repository to /var/www/newproject I was trying to configure nginx so a request to http://xxx.xxx.xxx.xxx/newproject would root to /var/www/newproject which is a silex app.

I've searched the web and all the configs I've found don't solve my problem. Accessing any route, returns 404.

My entire config is like this:

server {
    listen 80;
    server_name xxx.xxx.xxx.xxx;

    access_log /var/log/myproject/nginx_access.log;
    error_log /var/log/myproject/nginx_error.log;

    proxy_connect_timeout 300s;
    proxy_read_timeout 300s;

    location /newproject {
        root /var/www;
        try_files $uri $uri/ =404;
        index index.php;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        #fastcgi_index index.php;
        fastcgi_param  SCRIPT_FILENAME /var/www/newproject/index.php;
        #fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        #fastcgi_param SCRIPT_FILENAME /var/www$fastcgi_script_name;
        #fastcgi_param SCRIPT_FILENAME /var/www/newproject$fastcgi_script_name;
        include fastcgi_params;
    }

    location / {
        root /opt/myproject;
        proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        if (!-f $request_filename) {
            proxy_pass http://xxx.xxx.xxx.xxx:5000;
            break;
        }
    }
}

What am I missing here?

Thank you.

UPDATE:

Tried a new config (as below) and now accessing http://xxx.xxx.xxx.xxx/newproject/SOME_PATH I have a 404.

open() "/var/www/newproject/SOME_PATH" failed (20: Not a directory)
open() "/var/www/newproject/index.php/SOME_PATH" failed (20: Not a directory)

location /newproject {
        root /var/www/;
        index index.php index.html index.htm;
        location ~ ^/newproject/(.+\.php)$ {
            try_files $uri =404;
            root /var/www/;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_param HTTPS off;
            fastcgi_index index.php;
            #fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            #fastcgi_param SCRIPT_FILENAME $document_root/index.php;
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            include /etc/nginx/fastcgi_params;
        }
        location ~* ^/newproject/(.+\.(jpg|jpeg|gif|css|png|js|ico|html|xml|txt))$ {
            root /var/www/;
        }

    }

Upvotes: 2

Views: 20800

Answers (2)

TheBelgarion
TheBelgarion

Reputation: 330

dont forget to check user:group for nginx && php-fpm

Error Message will be the same if php-fpm is still running "apache" user and you already switched the user "nginx" on nginx and on the http directory!

and reload both

Upvotes: 2

Henrique Gonçalves
Henrique Gonçalves

Reputation: 1582

So, I've managed to solve my problem following this Site Configuration for Nginx Sub-Folder

Now my config is:

server {
    listen 80;
    server_name xxx.xxx.xxx.xxx;

    root /opt/myproject;

    access_log /var/log/myproject/nginx_access.log;
    error_log /var/log/myproject/nginx_error.log;

    proxy_connect_timeout 300s;
    proxy_read_timeout 300s;

    location /newproject {
        root /var/www;
        index index.php index.html index.htm;
        rewrite ^/newproject/(.*)$ /newproject/$1 break;
        try_files $uri $uri/ /newproject/index.php?q=$uri&$args;

        location ~ .*\.php$ {
            include /etc/nginx/fastcgi_params;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        }
    }

    location / {
        root /opt/myproject/;
        proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        if (!-f $request_filename) {
            proxy_pass http://xxx.xxx.xxxx.xxx:5000;
            break;
        }
    }
}

Since newproject depends on myproject, I kept their config in the same file and the same nginx access and error log files.

Upvotes: 4

Related Questions