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

Docker+PHP环境下Nginx将localhost重定向至index.php/问题求助

解决NGINX+PHP Docker容器中URL自动重定向到/index.php/的问题

问题背景

搭建了运行PHP+NGINX的Docker容器,曾修改NGINX配置实现“文件/目录不存在时重定向至根目录index.php”,却出现无限循环添加/index.php/到URL的问题。恢复配置后,仍存在单次重定向至http://localhost/index.php/的现象,且移除自定义PHP路由代码后问题依旧。

当前NGINX配置:

server {
    listen 0.0.0.0:80;
    root /var/www/html;
    location / {
        index index.php index.html;
    }
    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass php:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root/$fastcgi_script_name;
    }
}

自定义PHP路由代码(存在语法错误):

<?php
    $reqParsed = parse_url($SERVER['REQUESTURI']);
    $viewsDir = '/static/views/';

    $manifest = jsondecode(fileget_contents(__DIR . '/v1/manifest.json'), true);

    switch ($reqParsed['path']) {
        case '':
        case '/':
            require __DIR . $viewsDir . 'home.php';
            break;

        case '/posts/':
            if (isset($_GET['id'])) {
                if (isset($manifest['posts'][$GET['id']])) {
                    require DIR . $viewsDir . 'posts/post-view.php';
                }else {
                    require DIR . $viewsDir . 'posts/post-404.php';
                }
            }else{
                require _DIR . $viewsDir . 'posts/posts.php';
            }
            echo $GET['id'];
            break;

        default:
            httpresponse_code(404);
            require __DIR . $viewsDir . '404.php';
    }
?>

解决方案

1. 修正NGINX配置,避免外部重定向

当前配置的index指令可能触发外部重定向,改用try_files实现内部转发,既满足“不存在的文件/目录路由到index.php”的需求,又不会修改URL:

server {
    listen 0.0.0.0:80;
    root /var/www/html;

    location / {
        index index.php index.html;
        # 优先访问文件/目录,不存在则内部转发到index.php,保留查询参数
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        include fastcgi_params;
        fastcgi_pass php:9000;
        fastcgi_index index.php;
        # 修复路径拼接错误,避免多余的斜杠导致文件路径无效
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        # 补充PATH_INFO相关参数,适配PHP路由需求
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
    }
}

2. 修复PHP代码中的语法错误

尽管你认为路由代码不是问题,但代码中存在多处语法错误,可能导致意外输出或错误,间接触发重定向:

  • $SERVER['REQUESTURI'] → $_SERVER['REQUEST_URI'](超全局变量拼写错误)
  • jsondecode → json_decode(函数名拼写错误)
  • $GET['id'] → $_GET['id'](超全局变量拼写错误)
  • DIR/_DIR → __DIR__(魔术常量拼写错误)
  • httpresponse_code → http_response_code(函数名拼写错误)

修正后的PHP代码示例:

<?php
    $reqParsed = parse_url($_SERVER['REQUEST_URI']);
    $viewsDir = '/static/views/';

    $manifest = json_decode(file_get_contents(__DIR__ . '/v1/manifest.json'), true);

    switch ($reqParsed['path']) {
        case '':
        case '/':
            require __DIR__ . $viewsDir . 'home.php';
            break;

        case '/posts/':
            if (isset($_GET['id'])) {
                if (isset($manifest['posts'][$_GET['id']])) {
                    require __DIR__ . $viewsDir . 'posts/post-view.php';
                } else {
                    require __DIR__ . $viewsDir . 'posts/post-404.php';
                }
            } else {
                require __DIR__ . $viewsDir . 'posts/posts.php';
            }
            echo $_GET['id'] ?? '';
            break;

        default:
            http_response_code(404);
            require __DIR__ . $viewsDir . '404.php';
    }
?>

3. 确保配置生效并清除缓存

  • 重启NGINX服务(Docker容器内执行nginx -s reload,或直接重启容器)
  • 用浏览器无痕模式测试,避免旧的重定向缓存干扰
  • 查看NGINX错误日志(可临时开启debug日志:error_log /var/log/nginx/error.log debug;),确认重定向的发起来源

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.23 16:47:06