You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Nginx+PHP-FPM下Matrix推送URL重写异常求助

Nginx重写Matrix推送URL到Nextcloud PHP应用的正确配置

你的问题根源在于:Nginx执行rewrite后,只是修改了内部的请求路径,但没有覆盖传递给PHP-FPM的REQUEST_URI变量,导致Nextcloud的PHP脚本仍然读取到原始的/_matrix/push/v1/notify,从而触发URI不匹配的错误。

以下是两种可行的解决方案:

方案一:在重写location中直接配置PHP-FPM参数

直接在目标location里处理PHP转发,显式设置REQUEST_URI为最终路径:

location = /_matrix/push/v1/notify {
    # 定义目标URI
    set $target_uri /nextcloud/index.php/apps/uppush/gateway/matrix;
    
    # 覆盖传递给PHP的REQUEST_URI变量
    fastcgi_param REQUEST_URI $target_uri;
    # 提取PATH_INFO(Nextcloud路由依赖该参数)
    set $path_info /apps/uppush/gateway/matrix;
    fastcgi_param PATH_INFO $path_info;

    # 以下是你原有Nextcloud配置中的PHP-FPM参数,按需调整
    fastcgi_pass unix:/run/php/php8.2-fpm.sock; # 替换为你的PHP-FPM套接字或地址
    fastcgi_param SCRIPT_FILENAME $document_root/nextcloud/index.php;
    include fastcgi_params;
}

方案二:修改全局Nextcloud PHP的location块

如果希望保持rewrite逻辑独立,可在处理Nextcloud PHP的location中确保使用当前重写后的$request_uri:

# 原重写规则保留
location = /_matrix/push/v1/notify {
    rewrite ^.*$ /nextcloud/index.php/apps/uppush/gateway/matrix last;
}

# 修改Nextcloud的PHP处理location
location ~ ^/nextcloud/index.php(/.*)?$ {
    # 其他原有配置(root、fastcgi_pass等)保持不变
    # 显式设置REQUEST_URI为当前重写后的路径
    fastcgi_param REQUEST_URI $request_uri;
    # 确保PATH_INFO正确传递
    fastcgi_param PATH_INFO $fastcgi_path_info;
    
    include fastcgi_params;
}

关键说明

  • PHP-FPM默认会使用请求初始的REQUEST_URI值,即使Nginx内部做了rewrite,必须显式覆盖这个参数才能让Nextcloud识别到新的URI。
  • PATH_INFO参数是Nextcloud路由系统的依赖,需要确保其值对应新URI中index.php之后的部分。

内容的提问来源于stack exchange,提问作者Alexander

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 10:07:40