doulv1760
2014-01-24 12:10
浏览 73
已采纳

Nginx别名与laravel4

My nginx configuration for laravel

 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:

Route::get('/', function()
{
    return View::make('index');
});

Route::get('x', function()
{
    return "alpha";
});

my problem is,"http://app.dev/lara/index.php" is working but "http://app.dev/lara" and lara/x is not working.

图片转代码服务由CSDN问答提供 功能建议

laravel的我的nginx配置

  server {
 listen  80; 
 server_name app.dev; 
 rewrite_log on; 
 root / var / www / l4 / angular; 
 index index.html; 
 
 location / {
#要尝试的URL,包括漂亮的URL  。
 try_files $ uri $ uri / /index.php?$query_string;

} 
 location / lara / {
 index index.php; 
 alias / var / www / l4 / public /; \  n} 
#删除尾部斜杠以请路由系统。
 if(!-d $ request_filename){
 rewrite ^ /(。+)/ $ / $ 1 permanent; 
} 
 location~ ^ / lara /  (。* \ .php)$ {
别名/ 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; 
 包括fastcgi_params; 
} 
} 
   
 
 

Laravel route:

  Route :: get('/  ',function()
 {
返回View :: make('index'); 
}); 
 
Route :: get('x',function()
 
 {
 
返回“alpha”  ; 
}); 
   
 
 

我的问题是,“ http://app.dev/lara/index.php ”正在运行但是“ http:// app.dev/lara “和lara / x无效。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doudou_3636 2014-01-24 22:58
    已采纳

    In a nutshell, make the following edits. An explanation of why is below.

    Replace

    try_files   $uri $uri/ /index.php?$query_string;
    

    with

    try_files   $uri $uri/ /lara/index.php?$query_string;
    

    Replace the last location directive with this

    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;
    }
    

    Restart nginx.

    Now the why. I spotted a couple of mistakes with your nginx config. First, /index.php?$query_string in the try_files directive should be /lara/index.php?$query_string, otherwise nginx will try a request like http://app.dev/lara as /var/www/l4/angular/index.php?, which leads no where (unless you have an index.php there, and even is it will be served as text, not through fpm).

    The second has to do with the location ~ ^/lara/(.*\.php)$ directive. I think restricting it to URIs that end with .php is wrong, because it won't work for http://app.dev/lara/x, which will make nginx only search for /var/www/l4/public/x, returning 404 of course. Changing the regex to ^/lara/(.*)$ should do the job of catching /lara/x. Now the fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; directive is erroneous because for http://app.dev/lara/x, SCRIPT_FILENAME is /var/www/l4/public/x/lara/x, and removing the $1 in the alias directive won't make it any better. Instead, make the fastcgi_param like this fastcgi_param SCRIPT_FILENAME "/var/www/l4/public/index.php";, remove the alias directive, it's useless now, then move include fastcgi_params; above the fastcgi_param so it won't override SCRIPT_FILENAME value.

    Done? Not yet :). Trying /lara/x will show a Laravel routing error, because it tries to find the route lara/x instead of x, this is because you're including fastcgi_params. Just add fastcgi_param REQUEST_URI /$1; after SCRIPT_FILENAME param directive. Now it should be working fine. Don't forget to restart nginx :).

    点赞 打赏 评论

相关推荐 更多相似问题