Heim  >  Artikel  >  Backend-Entwicklung  >  Laravel Nginx 除 `/` 外所有路由 404

Laravel Nginx 除 `/` 外所有路由 404

WBOY
WBOYOriginal
2016-06-06 20:38:581831Durchsuche

是这样的,我完成一个 Laravel 项目后,打算把它转移到 Nginx 上,之前图方便,所以在开发环境下都是 php -S localhost:1234 来启动 HTTP 服务器,并没有在 Nginx 下开发,服务器是 Ubuntu 14.10。

把整个项目复制到 /usr/share/nginx/html 后,修改 app/storage 权限为 777,然后浏览器访问 / 可以访问,但除了 / 路由其他路由全部返回 404,比如 /home 返回 404 NOT FOUND,而在开发时是没有这些问题的。数据库这些都没问题,因为开发是在同一台机上。

下面是我的 Nginx 配置文件:

<code>config</code><code>server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /usr/share/nginx/html;
    index index.php index.html index.htm;

    server_name localhost;

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

    error_page 404 /404.html;

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}
</code>

我在 stackoverflow 看到了同样的问题,于是试着修改我的 Nginx 配置文件添加上:

<code>config</code><code>try_files $uri $uri/ @rewrite;

location @rewrite {
    rewrite ^/(.*)$ /index.php?_url=/$1;
}
</code>

还是 404,未能解决。

后来我又在 Nginx 根目录下新建了一个 Laravel 项目,修改 app/storage 权限后访问根路由正常,而访问其他自定义路由同样 404,比如:

app/route.php

<code>php</code><code>Route::get('home', function()
{
    return View::make('hello');
});
</code>

水平实在菜,真是无计可施了,请高手解疑,非常感谢。

回复内容:

是这样的,我完成一个 Laravel 项目后,打算把它转移到 Nginx 上,之前图方便,所以在开发环境下都是 php -S localhost:1234 来启动 HTTP 服务器,并没有在 Nginx 下开发,服务器是 Ubuntu 14.10。

把整个项目复制到 /usr/share/nginx/html 后,修改 app/storage 权限为 777,然后浏览器访问 / 可以访问,但除了 / 路由其他路由全部返回 404,比如 /home 返回 404 NOT FOUND,而在开发时是没有这些问题的。数据库这些都没问题,因为开发是在同一台机上。

下面是我的 Nginx 配置文件:

<code>config</code><code>server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /usr/share/nginx/html;
    index index.php index.html index.htm;

    server_name localhost;

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

    error_page 404 /404.html;

    error_page 500 502 503 504 /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}
</code>

我在 stackoverflow 看到了同样的问题,于是试着修改我的 Nginx 配置文件添加上:

<code>config</code><code>try_files $uri $uri/ @rewrite;

location @rewrite {
    rewrite ^/(.*)$ /index.php?_url=/$1;
}
</code>

还是 404,未能解决。

后来我又在 Nginx 根目录下新建了一个 Laravel 项目,修改 app/storage 权限后访问根路由正常,而访问其他自定义路由同样 404,比如:

app/route.php

<code>php</code><code>Route::get('home', function()
{
    return View::make('hello');
});
</code>

水平实在菜,真是无计可施了,请高手解疑,非常感谢。

php -S 有一个致命问题就是路由中的 'home' 必须写成 '/home' 才能工作。

详见 http://lvwenhan.com/php/406.html

不过你这个问题看起来是定向入口文件的问题,就是说没有把所有非静态请求都发送给入口文件 index.php。最后别忘了把根目录指向 laravel/public

Laravel 是什么我不知道,不过看起来它不是 WordPress 那种直接解析 URI 的,而是使用 PATH_INFO 来决定展现什么内容。

你把请求 rewrite 到 PHP 入口文件上试试?像这样子:

<code>location / {
    rewrite ^/(/.*)?$ /index.php$1 last;
}

location = / {
    index index.php;
}

location ~ (.+\.php\d?)($|/) {
    fastcgi_pass    unix:/run/php-fpm/php-fpm.sock;
    fastcgi_index   index.php;
    set $script $request_filename;
    if ($request_filename ~ ^(.+\.php\d?)(/.*)$){
            set $script $1;
            set $pathinfo   $2;
    }
    fastcgi_param   PATH_INFO   $pathinfo if_not_empty;
    fastcgi_param   SCRIPT_FILENAME $script;
    include     fastcgi_params;
}
</code>

<code>location / {
    try_files $uri $uri/ /index.php?$query_string;
}
</code>

详见Laravel官方文档:
http://laravel.com/docs/5.0/installation#pretty-urls

<code>location / {
    try_files $uri $uri/ /index.php;
}
加一个 root 试试

location / {
     root /var/www/
     try_files $uri $uri/ /index.php;
}
</code>

我用的iis8 Win8 遇到这个404 的问题了,给iis装了Url Rewrite组件导入 htacss 文件后就好了

try_files $uri =404;
把这行注释掉呢?
http://www.yimiju.com/articles/593.html

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn