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

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.

  • 写回答

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 :).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line