我在nginx服务器上遇到了laravel的路由问题。 它显示了一个“404找不到”的所有路线,除了默认的,非常类似于这个问题: https : //stackoverflow.com/questions/21091405/nginx-configuration-for-laravel-4
解决这个问题的方法:
服务器{
听80;
server_name sub.domain.com;
设置$ root_path'/ srv / www / htdocs / application_name / public';
root $ root_path;
index index.php index.html index.htm;
try_files $ uri $ uri / @rewrite;
位置@rewrite {
重写^ /(。*)$ /index.php?_url=/$1;
}
位置〜\ .php {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index /index.php;
包括/ etc / nginx / fastcgi_params;
fastcgi_split_path_info ^(。+ \。php)(/.+)$;
fastcgi_param PATH_INFO $ fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $ document_root $ fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $ document_root $ fastcgi_script_name;
}
位置〜* ^ /(css | img | js | flv | swf |下载)/(。+)$ {
root $ root_path;
}
location〜/\.ht {
否认一切;
}
}
它运作良好,唯一的问题是,我需要root_path是/ srv / www / htdocs,因为我有其他项目运行在那里。 每当我改变行:
set $root_path '/srv/www/htdocs/application_name/public';
至:
set $root_path '/srv/www/htdocs';
这个问题从url开始:localhost / application_name / public / users
您需要更改@rewrite块来分隔应用程序中的应用程序名称和URL。
尝试这个:
location @rewrite { rewrite ^/(?<appname>[^/]+)/public/(?<appurl>.+)$ /$appname/public/index.php?_url=/$appurl last; }
所以,在这里我们把请求URI的第一部分捕获到$ appnamevariables,然后把应用程序里面的URL变成$ appurlvariables。 然后我们使用这两个variables来重写对文件系统上真实URL的访问。
BTW。 你根本不需要set $root_path行,你可以直接使用root /srv/www/htdocs 。 你也可以完全删除location ~* ^/(css|img|js|flv|swf|download)/(.+)$ block,因为里面没有重新设置root指令。