LoginSignup
30
36

More than 5 years have passed since last update.

Laravelをサブディレクトリで動かす

Last updated at Posted at 2015-01-16

参考

概要

すでに既存のシステムがexample.comとして稼働している状態で、example.com/laraveltestにアクセスした場合は、処理をlaravelに任せたい。
基本的にはnginxのconfを修正するのみ。

なお、LinuxMintの場合、nginxのデフォルト設定は/etc/nginx/sites-available/defaultとなっている。(apt-getでインストールした場合)

実際のconfの内容

以下のlocation ~ ^/laraveltest((/)?(.+))?$ {で始まっているlocationディレクティブを追加するのみ。
追加したらnginx -s reloadで再起動すれば、example.com/laraveltest にアクセスするとちゃんとlaravelアプリケーションが動作する。

/etc/nginx/sites-available/default
server {

    #...省略

    #ここを追加する。
    location ~ ^/laraveltest((/)?(.+))?$ {

        root /var/www/nginx/laraveltest/public;

        try_files $1 /laraveltest/index.php?$query_string;

        location  ^/laraveltest/index.php$ {
            include fastcgi_params;
            #try_files                $uri = 404;
            include                  fastcgi_params;
            fastcgi_param            SCRIPT_FILENAME $document_root/index.php;
            fastcgi_param            PATHINFO        $fastcgi_path_info;
            fastcgi_param            PATH_TRANSLATED $document_root$fastcgi_path_info;
            fastcgi_split_path_info  ^(.+¥.php)(/.+)$;
            fastcgi_pass            127.0.0.1:9000;
            fastcgi_index index.php;
        }
    }
    # ... 省略
}

ディレクトリ名とURLを分けたい場合

URLは/laraveltestにしたいけど、Laravel自体はhogeというディレクトリにあるんだよね〜という場合は、単純にrootディレクティブを以下のように変更するのみ。

/etc/nginx/sites-available/default
root /var/www/nginx/hoge/public
30
36
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
30
36