首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 網頁程式設計 >> PHP 程式設計 >> PHP 相關知識

Nginx + ThinkPHP 解決 PATH_INFO 不支援與 404/500 錯誤完整配置指南

     編輯:PHP 相關知識

Nginx + ThinkPHP 解決 PATH_INFO 不支援與 404/500 錯誤完整配置指南

針對 ThinkPHP 3.x/5.x 在 Nginx 下 PATH_INFO 模式失效、頻繁 404/500 的根因分析與標準化修正。

核心問題:Nginx 預設不解析 PATH_INFO

ThinkPHP 依賴 PATH_INFO 解析模組/控制器/方法,Nginx 需額外配置才能正確提取。

標準 Nginx 配置(不需改 URL_MODEL)

server {
    listen 80;
    server_name your-domain.com;
    root /var/www/project/public;  # ThinkPHP 5/6 public 目錄
    index index.php index.html;

    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }

    location ~ \.php$ {
        fastcgi_pass unix:/run/php/php8.1-fpm.sock;  # 或 127.0.0.1:9000
        fastcgi_index index.php;
        include fastcgi_params;
        
        # 關鍵:正確提取 PATH_INFO
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }

    # 安全:隱藏敏感目錄
    location ~ /\. { deny all; }
    location ~ /(runtime|application)/ { deny all; }
}

關鍵參數解析

參數說明
try_files優先嘗試靜態檔案,否則交給 index.php 處理(支援 PATH_INFO)
fastcgi_param PATH_INFO將 Nginx 解析的 path_info 傳給 PHP,ThinkPHP 即可正確路由
fastcgi_param SCRIPT_FILENAME確保 PHP 接收正確的腳本絕對路徑

ThinkPHP 3.2 相容寫法(舊專案)

location / {
    if (!-e $request_filename) {
        rewrite ^/(.*)$ /index.php/$1 last;
    }
}

location ~ \.php {  # 注意:去掉 $ 結尾
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_index index.php;
    include fastcgi_params;
    
    set $path_info "";
    set $real_script_name $fastcgi_script_name;
    if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") {
        set $real_script_name $1;
        set $path_info $2;
    }
    fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
    fastcgi_param SCRIPT_NAME $real_script_name;
    fastcgi_param PATH_INFO $path_info;
}

關鍵細節:location ~ \.php(無 $),讓 /index.php/foo/bar 也能匹配進入 PHP 處理。

入口檔案補強(雙重保險)

// public/index.php
define('_PHP_FILE_', $_SERVER['SCRIPT_NAME']);
// 或 ThinkPHP 5+
// $_SERVER['PATH_INFO'] = $_SERVER['REQUEST_URI']; // 某些情況下需要

常見 404/500 排查清單

  1. 權限問題:runtime/、cache/ 目錄需 www-data 寫入權限
  2. PHP 版本不符:ThinkPHP 5.1 需 PHP 5.6+,6.0 需 PHP 7.1+
  3. 擴展缺失:pdo_mysql、mbstring、curl、openssl
  4. 快取殘留:部署後執行 php think clear 或刪除 runtime/cache/
  5. Nginx 錯誤日誌:tail -f /var/log/nginx/error.log 即時觀察

驗證 PATH_INFO 是否生效

// 路由測試:訪問 https://domain.com/index.php/index/index/hello/world
// 控制器
public function hello($name) {
    dump($_SERVER['PATH_INFO']); // 應輸出 /index/index/hello/world
    return 'Hello, ' . $name;
}

標準化 Nginx 配置 + 正確目錄權限 + 清除快取,能解決 99% 的 ThinkPHP 部署問題。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved