"File not found." for clean URLS

MagePsycho asked:

I’ve installed the nginx + php-fpm + mysql stack in my Mac using homebrew.

MacOsX: 10.9.2
Nginx Version: 1.6.1
Document Root: /Users/raj/Sites/
Project Dir: /Users/raj/Sites/projects/newproject

My /usr/local/etc/nginx/nginx.conf configuration looks like:

user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;

events {
    worker_connections  1024;

http {
    include         mime.types;
    #include            /usr/local/etc/nginx/conf.d/*.conf;
    sendfile        on;
    keepalive_timeout  65;

    gzip  on;
    # gzip_disable "MSIE [1-6]\.(?!.*SV1)";

    server {
        listen       80;
        server_name  localhost;
        root  /Users/raj/Sites;

        location / {
            try_files $uri $uri/ /index.php;
            index  index.php;
            autoindex on;

        # configure *.PHP requests
        location ~ \.php$ {
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;

If I open the url: or it works fine.

But if i open any other url or even then it gives me the following error:

File not found.

And the error.log says:

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


If I add the following lines in ‘location ~ .php$ {‘ block:

try_files       $uri /index.php;

gives the error:

rewrite or internal redirection cycle while internally redirecting to "/index.php"

Additionally I found the following error log when trying to restart the FPM:

[pool www] 'user' directive is ignored when FPM is not running as root
[pool www] 'group' directive is ignored when FPM is not running as root

My answer:

Your configuration has two obvious problems:

  1. Your location @handler is completely unnecessary and redundant. This is already covered by your existing index and try_files directive. Remove that location entirely and fix try_files.

    try_files $uri $uri/ /index.php;
  2. You have placed your root directives in the wrong places.

    root should be defined in the server block, not in each location block. This is one of the most common nginx misconfigurations.

    Remove all of the root directives from each location block, and place the correct root directive within the server block, not within any location.

Once you’ve gotten your configuration to this baseline, you should find things start working again, or you might have a different issue which is easier to deal with.

View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.