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

Nginx同一域名部署WordPress与TYPO3:特定路径跳转异常排查

同一域名下WordPress与TYPO3共存的Nginx配置问题

因业务需求,需在同一域名下搭建基于WordPress与TYPO3的网站,以下是当前Nginx配置:

server {
    listen 80;
    server_name example.com;

    access_log /var/www/access.log;
    error_log /var/www/error.log;

    index index.html index.htm index.php;

    location / {
        root /var/www/wordpress;

        try_files $uri $uri/ /index.php?$args;

        location ~ \.php$ {
            try_files $uri =404;

            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_pass unix:/run/php/php8.3-fpm.sock;
            fastcgi_index index.php;

            include fastcgi_params;
        }
    }
    location ~ ^/(sign-up|login|others) {
        root /var/www/typo3;

        try_files $uri /index.php$is_args$args;

        location ~ [^/]\.php(/|$) {
            fastcgi_split_path_info ^(.+?\.php)(/.*)$;
            if (!-f $document_root$fastcgi_script_name) {
                return 404;
            }

            fastcgi_buffer_size 32k;
            fastcgi_buffers 8 16k;
            fastcgi_connect_timeout 240s;
            fastcgi_read_timeout 240s;
            fastcgi_send_timeout 240s;

            fastcgi_param HTTP_PROXY "";
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_pass unix:/run/php/php8.3-fpm.sock;
            fastcgi_index index.php;

            include fastcgi_params;
        }
    }
}

配置逻辑为:大部分请求由WordPress处理,仅/sign-up、/login、/others等特定路径需由TYPO3处理。但实际访问http://example.com/sign-up时,页面跳转至WordPress而非TYPO3;若将根目录改为/var/www/typo3则可正常访问TYPO3的注册页面。


问题原因

核心问题出在TYPO3 location中try_files指令的路径解析规则:

  1. 当try_files使用以/开头的路径(如/index.php)时,Nginx会基于server块的全局root查找文件。由于你的server块未设置全局root,Nginx会 fallback 到第一个匹配的location的root(即WordPress的/var/www/wordpress),导致跳转至WordPress的入口文件。
  2. 尽管正则location(~ ^/(sign-up|login|others))优先级高于前缀location(/),但try_files触发的/index.php请求会重新匹配location,最终命中WordPress的location ~ \.php$,由WordPress处理。

解决方案

修改TYPO3 location中的try_files指令,使用相对路径或明确指定基于当前location root的路径,确保指向TYPO3的入口文件:

server {
    listen 80;
    server_name example.com;

    access_log /var/www/access.log;
    error_log /var/www/error.log;

    index index.html index.htm index.php;

    location / {
        root /var/www/wordpress;

        try_files $uri $uri/ /index.php?$args;

        location ~ \.php$ {
            try_files $uri =404;

            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_pass unix:/run/php/php8.3-fpm.sock;
            fastcgi_index index.php;

            include fastcgi_params;
        }
    }
    location ~ ^/(sign-up|login|others) {
        root /var/www/typo3;

        # 修正:使用相对路径的index.php,基于当前location的root拼接
        try_files $uri $uri/ index.php$is_args$args;

        location ~ [^/]\.php(/|$) {
            fastcgi_split_path_info ^(.+?\.php)(/.*)$;
            if (!-f $document_root$fastcgi_script_name) {
                return 404;
            }

            fastcgi_buffer_size 32k;
            fastcgi_buffers 8 16k;
            fastcgi_connect_timeout 240s;
            fastcgi_read_timeout 240s;
            fastcgi_send_timeout 240s;

            fastcgi_param HTTP_PROXY "";
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
            fastcgi_pass unix:/run/php/php8.3-fpm.sock;
            fastcgi_index index.php;

            include fastcgi_params;
        }
    }
}

补充说明

  • 相对路径的index.php会自动基于当前location的root(/var/www/typo3)拼接为/var/www/typo3/index.php,正确指向TYPO3的入口。
  • 也可以显式写成try_files $uri $uri/ $document_root/index.php$is_args$args;,效果一致,逻辑更清晰。
  • 确保TYPO3的路由配置已正确配置,可处理/sign-up这类伪静态路径。

内容的提问来源于stack exchange,提问作者Bill.Dagou

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 06:12:34