大网数据广告活动
服务器运维问答

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

· 52CE 我爱测

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…

问题原文

原帖链接 · Axel Nilsson · CC BY-SA 3.0

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.

已采纳解答

原帖链接 · Richard Smith · CC BY-SA 3.0

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.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-02-16)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。