When setting up DAViCal with Nginx and PHP-FPM, many developers encounter the frustrating "File not found" error with the accompanying FastCGI error message: "Primary script unknown". This typically occurs when using alias directives in Nginx configuration while trying to serve PHP applications.
The core issue lies in how Nginx handles the SCRIPT_FILENAME
parameter when using alias directives with PHP-FPM. The standard configuration using $request_filename
doesn't properly resolve the correct file path when aliases are involved.
# Problematic default setting in fastcgi_params
fastcgi_param SCRIPT_FILENAME $request_filename;
Here are two working approaches to solve this issue:
Solution 1: Modify the PHP location block
Update your PHP location block to properly handle aliased paths:
location ~ \\.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_split_path_info ^(.+\\.php)(/.+)$;
# Critical fix: Use $document_root$fastcgi_script_name instead of $request_filename
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
}
Solution 2: Specific configuration for the aliased directory
For the DAViCal specific location, add a dedicated PHP handler:
location /kalender {
alias /usr/share/davical/htdocs;
location ~ \\.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $request_filename;
fastcgi_pass unix:/var/run/php5-fpm.sock;
}
try_files $uri $uri/ /kalendar/index.php;
}
When implementing these solutions, ensure:
- Your PHP-FPM pool has proper permissions to access the files
- The alias path points to the correct directory
- Files in the target directory have appropriate permissions (typically www-data:www-data)
- You restart both Nginx and PHP-FPM after configuration changes
Create a simple test PHP file in your aliased directory:
echo "<?php phpinfo(); ?>" | sudo tee /usr/share/davical/htdocs/info.php
sudo chown www-data:www-data /usr/share/davical/htdocs/info.php
Then access it through your browser at http://your-server/kalendar/info.php to verify everything works correctly.
When setting up DAViCal with Nginx, I encountered the frustrating "File not found" error with the following message in the logs:
FastCGI sent in stderr: "Primary script unknown"
Here's the relevant part of my Nginx configuration that was causing issues:
location /kalender {
alias /usr/share/davical/htdocs;
}
location ~ \.php$ {
include /etc/nginx/fastcgi_params;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
}
The issue occurs because when using alias
with PHP-FPM, Nginx passes the wrong SCRIPT_FILENAME
to PHP-FPM. The default fastcgi_param SCRIPT_FILENAME $request_filename
doesn't work correctly with alias directives.
Here's the corrected configuration that worked for me:
location /kalender {
alias /usr/share/davical/htdocs;
location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
}
}
- Nested the PHP location block inside the alias location
- Modified
SCRIPT_FILENAME
to use$document_root$fastcgi_script_name
- Removed
fastcgi_split_path_info
as it wasn't needed in this case
Here's the full server block that worked successfully:
server {
listen 80;
listen [::]:80 default_server ipv6only=on;
root /var/www-data;
index index.html index.htm index.shtml index.php;
server_name just.a.server;
location / {
try_files $uri $uri/ /index.html;
ssi on;
}
location /kalender {
alias /usr/share/davical/htdocs;
location ~ \.php$ {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
}
}
}
Make sure:
- PHP-FPM is running and listening on the correct socket
- File permissions are set correctly for the DAViCal files
- Nginx worker process has read access to the files