Nginx无法读取不存在的laravel的.php路由
我用“.php”扩展创建了一些Laravel路由,例如Nginx无法读取不存在的laravel的.php路由,php,laravel,nginx,Php,Laravel,Nginx,我用“.php”扩展创建了一些Laravel路由,例如 Route::get('/api/send.php', function(){ echo 'Hi There'; }); 但当我打开路由时,它显示Nginx服务器中的404错误。。。这就是Nginx配置 server { listen 80 default_server; listen [::]:80 default_server; root /var/www/laravel/public; #
Route::get('/api/send.php', function(){
echo 'Hi There';
});
但当我打开路由时,它显示Nginx服务器中的404错误。。。这就是Nginx配置
server {
listen 80 default_server;
listen [::]:80 default_server;
root /var/www/laravel/public;
# Add index.php to the list if you are using PHP
index index.php index.html index.htm index.nginx-debian.html;
server_name xxx.xxx.xxx.xxx;
location / {
# First attempt to serve request as file, then
# as directory, then fall back to displaying a 404.
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
}
location ~ /\.ht {
deny all;
}
}
有什么问题?我使用的代码与以前的代码相同
谢谢
已解决
它通过改变来解决
try_files $fastcgi_script_name =404;
在/etc/nginx/snippets/fastcgi-php.conf中
try_files $fastcgi_script_name /index.php?$query_string;
这是配置为服务于PHP应用程序(尤其是Laravel)的nginx的一个非常常见的问题。你可以在像laravel.com这样的网站上复制它,例如: 默认配置(您可能在
snippets/fastcgi php.conf
中使用的配置)如下:
而且
请求URI,或者,如果URI以斜杠结尾,则请求URI附带由fastcgi_index指令配置的索引文件名。此变量可用于设置脚本文件名和路径转换参数,这些参数在PHP中确定脚本名称。例如,对于带有以下指令的“/info/”请求
这意味着,当请求URI包含.php
时,它被视为对php文件的请求,如果该php文件不存在,nginx将返回一个错误——它永远不会到达您的应用程序
解决方案是强制fastcgi\u script\u name
始终等于应用程序的入口点,在本例中为index.php
。您可以编辑snippets/fastcgi php.conf
或将其添加到位置块中,如下所示:
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root/index.php;
}
您的应用程序现在将接收每个请求,包括路径中包含
.php
的请求。我刚刚尝试了该代码,我得到了带有消息“Hi There”的页面。您的设置有问题。我正在使用ubuntu服务器18,我不知道您认为我应该检查什么?通过更改替换try\u files$fastcgi\u script\u name=404
totry\u files$fastcgi\u script\u name/index.php?$query\u string代码>
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root/index.php;
}