我有一个部署了 LEMP 堆栈的服务器,它正在运行一个 laravel 应用程序。 当我遇到错误(到目前为止为 404 或 419)时,我看到的是 nginx 错误页面,而不是 laravel 样式的错误页面。 知道如何解决这个问题或者这是否按设计工作?
您需要在 nginx 配置文件中添加:
error_page 404 /index.php;
完整示例:
server {
listen 80;
listen [::]:80;
server_name example.com;
root /srv/example.com/public;
add_header X-Frame-Options "SAMEORIGIN";
add_header X-Content-Type-Options "nosniff";
index index.php;
charset utf-8;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location = /favicon.ico { access_log off; log_not_found off; }
location = /robots.txt { access_log off; log_not_found off; }
error_page 404 /index.php;
location ~ \.php$ {
fastcgi_pass unix:/var/run/php/php8.2-fpm.sock;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
include fastcgi_params;
fastcgi_hide_header X-Powered-By;
}
location ~ /\.(?!well-known).* {
deny all;
}
}
请查看 Laravel 文档。 https://laravel.com/docs/11.x/deployment#nginx