Mahesh.D
Mahesh.D

Reputation: 1689

Nginx - Primary script unknown while reading response header from upstream

Recently we have migrated from Apache2 to Nginx server. Consider we have domain www.test.com and following is the www.test.com.conf and I had disabled default Nginx default file.

server {
    server_name www.test.com;

    # Character Set
    charset utf-8;

    # Logs
    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    # Directory Indexes
    index index.html index.htm index.php;

    # Document Root
    root /var/www/html/project1/public;

    # Location
    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }

    # Error Pages
    error_page 404 /404.html;
    error_page 500 502 503 504 /50x.html;

    # PHP-FPM Support
    location ~ \.php$ {
        fastcgi_read_timeout 240;
        fastcgi_pass unix:/var/run/php/php7.2-fpm.sock; #/var/run/php5-fpm.sock;
        #include fastcgi.conf;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    # Block access to .htaccess
    location ~ \.htaccess {
        deny all;
    }
    client_body_timeout 10s;
    client_header_timeout 10s;
    client_max_body_size 100M;

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/www.test.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/www.test.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot
}
server {
    if ($host = www.test.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot


    listen *:80;
    server_name www.test.com;
    return 404; # managed by Certbot

}

With the above configuration, I can access https://www.test.com without issues. In this case root /var/www/html/project1/public. Now to access multiple applications from the same domain I had changed the root directive to /var/www/html/ and tried to access https://www.test.com/project1/public but I'm getting the following error

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

May I know the reason for this issue? my application is Lumen which is a mirco service framework by Laravel.

Upvotes: 0

Views: 3287

Answers (1)

Mahesh.D
Mahesh.D

Reputation: 1689

By changing the following blocks it is working for me

from root /var/www/html/project1/public; to root /var/www/html;

And we need to add multiple location blocks based on the requirement. Consider I want to access two Lumen/ Laravel applications from single domain, then I need to add two location blocks as

location /project1/public {
  try_files $uri $uri/ /project1/public/index.php$is_args$args;
}
location /project2/public {
  try_files $uri $uri/ /project2/public/index.php$is_args$args;
}

Credits go to Richard Smith

Upvotes: 1

Related Questions