[英]Nginx alias with laravel4
我的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;
}
}
Laravel路線:
Route::get('/', function()
{
return View::make('index');
});
Route::get('x', function()
{
return "alpha";
});
我的問題是,“ http://app.dev/lara/index.php ”正在工作,但是“ http://app.dev/lara ”和lara / x無法工作。
簡而言之,進行以下編輯。 下面是為什么的解釋。
更換
try_files $uri $uri/ /index.php?$query_string;
與
try_files $uri $uri/ /lara/index.php?$query_string;
以此替換最后一個位置指令
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;
}
重新啟動nginx。
現在為什么。 我發現您的Nginx配置存在一些錯誤。 首先, /index.php?$query_string
在try_files
指令應/lara/index.php?$query_string
,否則nginx的將嘗試請求等http://app.dev/lara
如/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;
指令是錯誤的,因為對於http://app.dev/lara/x
, SCRIPT_FILENAME
是/var/www/l4/public/x/lara/x
,並移除$1
的別名指令將不能使它更好。 相反,將fastcgi_param
如下形式: fastcgi_param SCRIPT_FILENAME "/var/www/l4/public/index.php";
,刪除alias指令,現在它已無用,然后移動include fastcgi_params;
在fastcgi_param
上方,因此不會覆蓋SCRIPT_FILENAME
值。
做完了嗎 還沒 :)。 嘗試/lara/x
將顯示Laravel路由錯誤,因為它試圖找到路由lara/x
而不是x
,這是因為您包括fastcgi_params
。 只需添加fastcgi_param REQUEST_URI /$1;
在SCRIPT_FILENAME
參數指令之后。 現在應該可以正常工作了。 不要忘記重啟nginx :)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.