Pierre Le Bot
Pierre Le Bot

Reputation: 324

Nginx sites-available doesn't work

I've got this really simple server block under sites-available.

Problem: When I try to access to mydomain.com, Nginx returns a « 404 Not Found », but if I try to access to a file in particular, it works fine, like mydomain.com/index.php

server {

    listen          80;
    index           index.php;
    server_name     mydomain.com;
    root            /home/myusername/sites/mydomain.com/htdocs;

    access_log      /home/myusername/sites/mydomain.com/logs/access.log;
    error_log       /home/myusername/sites/mydomain.com/logs/error.log;

    location / {
            try_files $uri =404;
    }

    location ~ \.php$ {
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            include fastcgi_params;
    }

}

Note that:

Upvotes: 2

Views: 252

Answers (1)

Pierre Le Bot
Pierre Le Bot

Reputation: 324

So, the answer was giver to me on ServerFault by Alexey Ten, here is a copy of the answer


Your try_files directive is too restrictive and, I guess, is in wrong place.

Either remove location / completely, it doesn't makes much sense, or, at least add $uri/ so index directive will work.

try_files $uri $uri/ =404;

But my guess is, you need to move this try_files into location ~ \.php$, this will make sure that php-file exsists before pass it to PHP-FPM for processing. All other files will be served by nginx with proper use of index directive.

server {
    listen          80;
    index           index.php;
    server_name     mydomain.com;
    root            /home/myusername/sites/mydomain.com/htdocs;

    access_log      /home/myusername/sites/mydomain.com/logs/access.log;
    error_log       /home/myusername/sites/mydomain.com/logs/error.log;

    location ~ \.php$ {
            try_files $uri =404;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            include fastcgi_params;
    }
}

Upvotes: 1

Related Questions