2

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.

1 Answer 1

4

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.

2
  • After two days of trying, it finally worked! I don't know how I'll be able to thank you, but thank you! :) Commented Feb 16, 2016 at 21:16
  • +1, this worked for me on the non-beta Flarum and php7.4-fpm. The 'admin' and 'api' location sections are no longer needed. Note, if you include the .nginx.conf file that ships with flarum be sure to comment out the location / stanza at the top with the try_files (since it's now included in this answer) and uncomment the suggested lines that send a 404 when accessing non-public files, you need to change location ~* ^/( ... ) to location ~* ^/forum/( ... ) Commented Dec 29, 2021 at 2:05

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.