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

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

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

下面是我的 Nginx 配置文件:
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;
}
}

我在 stackoverflow 看到了同样的问题,于是试着修改我的 Nginx 配置文件添加上:
try_files $uri $uri/ @rewrite;

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

还是 404,未能解决。

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

app/route.php
Route::get('home', function()
{
return View::make('hello');
});

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

FrankTung - Think big. Think different.

赞同来自: FiveSay

我目前使用的是centos+nginx。
几个关键点:
1)root要指向public文件夹
2)使用"pretty" URLs。官方文档:http://laravel.com/docs/4.2/in ... -urls
location / {
try_files $uri $uri/ /index.php?$query_string;
}

贴一下我的配置:<br />
server {
listen 8040;
server_name yourdomain.com;
root /path/to/your/laravel/project/public;
index index.html index.php;

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

location ~ \.php$ {
    try_files $uri =404;
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    include fastcgi_params;
}
}

Hope it could help you.

G8Ycn

赞同来自: FiveSay

问题已经解决!
public下的.htaccess文件无需更改。首先需要在httpd.conf文件中开启虚拟主机,然后在httpd-vhosts.conf配置虚拟主机如下($USER为你的Mac用户名):
<VirtualHost *:80>
DocumentRoot "/Users/$USER/Sites/laravel-4.1-simple-blog/public"
ServerName sites
ErrorLog "/private/var/log/apache2/sites-error_log"
CustomLog "/private/var/log/apache2/sites-access_log" common
<Directory "/Users/$USER/Sites/">
Options Indexes MultiViews FollowSymLinks
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
打开hosts配置文件,加入"127.0.0.1 sites" 后就可以通过http://sites访问了!
之前通过“http://[本地IP]/~[用户名]”来访问“/Users/[用户名]/Sites/”目录的方式是行不通的,可能跟laravel机制有关。

FiveSay - 成武

赞同来自:

  1. 确认已经安装并开启了 Nginx 的 Rewrite 模块。
  2. 确认 Rewrite 规则书写的位置是否正确。

G8Ycn

赞同来自:

我也遇到了这个问题,我用的是apache,可以确认的是Rewrite已经开启
RSN2ZBNJCZK{BRHO}P3HQ[5.jpg

.htaccess也按照文档说的换成如下版本
附上代码
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
#Options -MultiViews
Options +FollowSymLinks
</IfModule>
RewriteEngine On

# Redirect Trailing Slashes...
#RewriteRule ^(.*)/$ /$1 [L,R=301]

# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>

要回复问题请先登录注册