php - Nginx - 重定向 404 PHP 文件

标签 php linux ubuntu

我在本地使用 Laravel 的 valet。我在本地没有遇到这个问题。我的远程服务器使用 Ubuntu 16.04

我的网站根目录中有一个 index.php:

<?php

require __dir__ . '/src/core/bootstrap.php';
require __dir__ . '/src/controllers/index.php';

src/core/bootstrap.phpcomposerdatabases 的东西。但这就是 src/controllers/index.php 的内容:

<?php
session_start();

use App\UserTools\User;
use App\Core\Router;

$page = Router::load()->fetchPage();

include "src/controllers/{$page}.controller.php";
include "src/views/{$page}.view.php";

因此,当用户访问 site.com 时,它会转到 main,因为它是主页。但是,比方说,他们访问:site.com/about,那么 $page 将是 about 和 viola... 路由.这些都是在 Laracasts 上教给我的,所以如果这看起来……很初级,请原谅。

问题在于,当我访问 site.com/api 时,它只显示一个空白页面。或者,如 book?id=1 空白页。或者

这是来自 valetnginx block ,它告诉服务器如何处理找不到的文件:

location / {
    rewrite VALET_SERVER_PATH last;
}

如何将其应用到我的网站?我尝试用 /var/www/html 替换 VALET_SERVER_PATH 但我刚收到服务器错误 500。

这是我当前的 nginx block :

server {
    server_name www.site.org;
    return 301 $scheme://site.org$request_uri;
}

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    server_name site.org;
    return 301 https://$server_name$request_uri;
}

server {

    # SSL configuration
    listen 443 ssl default_server;
    listen [::]:443 ssl default_server;
    include snippets/ssl-site.org.conf;
    include snippets/ssl-params.conf;

    root /var/www/html;

    index index.php index.html index.htm index.nginx-debian.html;

    server_name site.org;

    location / {
        try_files $uri $uri/ /index.php;
    }

    location ~ /.well-known {
        allow all;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    }
}

它有效。但仅限于首页。是的,我启用了 HTTPS,www 流量被重定向到 非 www URI。

最佳答案

对以下指令进行更改:

location / {
    try_files $uri $uri/ /index.php$is_args$args;
}

location ~ \.php$ {
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
}

关于php - Nginx - 重定向 404 PHP 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43304017/

相关文章:

javascript - 单选按钮的不同值(如果选择或未选择)

Java native 接口(interface) GetStringUTFChars fatal error

linux - Liquidsoap 记录输出文件夹中的所有文件

php - 更新到 php8.0 时无法启动 PHP 8.0 FastCGI 进程

linux - Unix的时间计算软件

PHP CURL 使用 POST 原始 JSON 数据

php - 强制 Composer 更新包

php - 如何显示可读数组 - Laravel

linux - 在 while read (stdin) 循环后暂停 bash 脚本

linux - 我的代码给出错误 : ./greeting2.sh: line 11: syntax error: unexpected end of file