I have following nginx configuration:
location / {
try_files $uri $uri/ index.html =404;
if (!-e $request_filename) {
rewrite ^/(.+)$ index.php?url=$1 last;
}
}
location ~ .php$ {
# protection from known vulnerability
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
(fastcgi_params
are defaults from Debian package)
it works for request /
, however when the request is rewritten the main file is not found:
request is /contact
which should be rewritten to /index.php?url=contact
*104 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 10.0.0.1, server: localhost, request: "GET /contact HTTP/1.0", upstream: "fastcgi://unix:/var/run/php5-fpm.sock:", host: "localhost:8080"
I'm unable to get from logs what is actuall fastcgi trying to load, which path?
Note that
index.php
and/index.php
are different URIs. You've forgotten slashes in your rewrites.This is a better way to implement the same functionality:
and then your PHP location block as in your setup.
try_files
checks first if the file matching $uri exists, then a directory, and if neither exists, it uses the rewrite -location, which runs the script.Most likely reason your setup was not working was the missing
/
from the rewrite script path. Anyway, this setup is simpler is the preferred one with nginx.