Tux*_*Bot 1 php nginx laravel-4
laravel的我的nginx配置
server {
listen 80;
server_name app.dev;
rewrite_log on;
root /var/www/l4/angular;
index index.html;
location /{
# URLs to attempt, including pretty ones.
try_files $uri $uri/ /index.php?$query_string;
}
location /lara/ {
index index.php;
alias /var/www/l4/public/;
}
# Remove trailing slash to please routing system.
if (!-d $request_filename) {
rewrite ^/(.+)/$ /$1 permanent;
}
location ~ ^/lara/(.*\.php)$ {
alias /var/www/l4/public/$1;
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
Run Code Online (Sandbox Code Playgroud)
Laravel路线:
Route::get('/', function()
{
return View::make('index');
});
Route::get('x', function()
{
return "alpha";
});
Run Code Online (Sandbox Code Playgroud)
我的问题是," http://app.dev/lara/index.php "正在运行,但" http://app.dev/lara "和lara/x无效.
简而言之,进行以下编辑.下面解释原因.
更换
try_files $uri $uri/ /index.php?$query_string;
Run Code Online (Sandbox Code Playgroud)
同
try_files $uri $uri/ /lara/index.php?$query_string;
Run Code Online (Sandbox Code Playgroud)
用这个替换最后一个location指令
location ~ /lara/(.*)$ {
fastcgi_pass unix:/var/run/php5-fpm.sock;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME "/var/www/l4/public/index.php";
fastcgi_param REQUEST_URI /$1;
}
Run Code Online (Sandbox Code Playgroud)
重启nginx.
现在为什么.我发现你的nginx配置有几个错误.首先,/index.php?$query_string在try_files指令中应该是/lara/index.php?$query_string,否则nginx将尝试像http://app.dev/laraas /var/www/l4/angular/index.php?这样的请求,这导致没有位置(除非你有一个index.php,甚至它将作为文本,而不是通过fpm).
第二个与location ~ ^/lara/(.*\.php)$指令有关.我认为将其限制为以.php结尾的URI是错误的,因为它不起作用http://app.dev/lara/x,这将使nginx只搜索/var/www/l4/public/x,当然返回404.改变正则表达式^/lara/(.*)$应该做的就是抓住/lara/x.现在该fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;指令是错误的,因为for http://app.dev/lara/x,SCRIPT_FILENAMEis /var/www/l4/public/x/lara/x和remove $1中的alias指令不会使它更好.相反,做fastcgi_param这样的fastcgi_param SCRIPT_FILENAME "/var/www/l4/public/index.php";,删除别名指令,它现在没用,然后移动到include fastcgi_params;上面,fastcgi_param所以它不会覆盖SCRIPT_FILENAME值.
完成了吗?还没 :).尝试/lara/x将显示Laravel路由错误,因为它试图找到路由lara/x而不是x,这是因为你包括fastcgi_params.只需fastcgi_param REQUEST_URI /$1;在SCRIPT_FILENAMEparam指令后添加.现在它应该工作正常.别忘了重启nginx :).