Nginx – Flarum on a sub-directory along with Laravel (Nginx)

nginxphp-fpm

I've been trying to put Flarum on a sub-directory with Laravel being on the root directory. I've tried a number of things and the only response that I get from the server is either a 404, or the laravel index.php downloaded (when I visit /forum).

From what I can remember, I have tried:

  • Having an individual location ~\.php for every subdirectory.
  • Putting the php location in the sub-directory location.
  • Trying both root and alias in the location.

My setup is as follows:

  • Laravel files on /var/www/laravel/public/
  • Flarum files on /var/www/forum/
  • PHP5-FPM on /var/run/php5-fpm.sock

Laravel is been working through the whole process, but I can't manage to get flarum to work.

server {
    listen       80;
    server_name  website.web
    return         301 https://website.web$request_uri;
}

server {
    listen         443 ssl;
    server_name    website.web
    access_log /var/www/logs/access.log;
    error_log /var/www/logs/error.log warn;
    ssl_certificate /var/www/ssl/website.chained.crt;
    ssl_certificate_key /var/www/ssl/website.key;
    root /var/www/laravel/public/;
    index index.php;

    location / {
            try_files $uri $uri/ /index.php?$query_string;
    }

    location /forum/ {
            root /var/www/;
            try_files $uri $uri/ /index.php?$query_string;
    }

    location /forum/admin/ {
            root /var/www/;
            try_files $uri $uri/ /admin.php?$query_string;
    }

    location /forum/api/ {
            root /var/www/;
            try_files $uri $uri/ /api.php?$query_string;
    }

    location /flarum {
        deny all;
        return 404;
    }

    location ~ \.php$ {
            try_files $uri /index.php =404;
            fastcgi_split_path_info ^(.+.php)(/.+)$;
            fastcgi_pass unix:/var/run/php5-fpm.sock;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME     $document_root$fastcgi_script_name;
            include fastcgi_params;
    }
}

If the config wont work for some reason, then here's a pastebin link with it.

Best Answer

You have two applications with different document roots that both need PHP support. If you keep the Laravel configuration unchanged, you can use nested location blocks to implement the Flarum configuration.

Something like this might work:

location ^~ /forum {
    root /var/www;
    try_files $uri $uri/ /forum/index.php?$query_string;

    location ~ \.php$ {
        try_files $uri /forum/index.php;
        fastcgi_pass unix:/var/run/php5-fpm.sock;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    }

    location /forum/admin {
        try_files $uri $uri/ /forum/admin.php?$query_string;
    }

    location /forum/api {
        try_files $uri $uri/ /forum/api.php?$query_string;
    }
}

Note the following:

location ^~ /forum is a prefix location that takes precedence over the top level location ~ \.php$ which allows the nested location to process PHP scripts for Flarum.

The /forum/index.php is a URI and the /forum/ prefix is necessary to pick up the correct index.php file. You do not need a default URI and =404 on the try_files directive (one or the other please). I have removed fastcgi lines that do nothing. And include fastcgi_params before other fastcgi_param directives to avoid unintentional side-effects.

This is a generic solution and I haven't actually tested it with Laravel or Flarum.

See documentation here for more.