我在 sites-available 下有一个非常简单的 server 块。
问题:当我尝试访问 mydomain.com 时,Nginx会返回«404 Not Found»,但如果我尝试访问某个文件,它工作正常,如 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;
}
}
请注意:
答案 0 :(得分:1)
所以,答案是通过ServerFault Alexey Ten给我的答案,这里是答案的副本
您的try_files
指令限制性太强,而且我猜错了地方。
完全删除location /
,它没有多大意义,或者至少添加$uri/
,因此index
指令将起作用。
try_files $uri $uri/ =404;
但是我的猜测是,您需要将此try_files
移动到location ~ \.php$
,这将确保php文件存在,然后再将其传递给PHP-FPM进行处理。所有其他文件将由nginx提供,并正确使用index
指令。
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;
}
}