Backend Development 3 min read

Enabling PathInfo Mode for ThinkPHP on Nginx: Configuration and Rewrite Solutions

This guide explains why Nginx may not support ThinkPHP's PathInfo mode, and provides two detailed solutions—enabling PathInfo in the fastcgi configuration and using rewrite rules to hide index.php—along with the exact code snippets needed to apply them.

php中文网 Courses
php中文网 Courses
php中文网 Courses
Enabling PathInfo Mode for ThinkPHP on Nginx: Configuration and Rewrite Solutions

When running a ThinkPHP project on Apache, the server natively supports PathInfo mode, so all four ThinkPHP access modes work without issue. However, on Nginx this often fails because older versions lack PathInfo support and newer versions (≈0.7+) have it disabled by default.

Reason: Low‑version Nginx does not support PathInfo; higher‑version Nginx supports it but the feature is not turned on.

Solution 1 – Enable PathInfo in Nginx: In the site configuration locate the location ~ \.php$ { ... } block, remove the trailing $ symbol, and add the following directives inside the braces:

fastcgi_split_path_info ^(.+\.php)(.*)$;

fastcgi_param PATH_INFO $fastcgi_path_info;

fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;

Save the file and restart Nginx. The server will now support PathInfo, allowing URLs such as www.example.com/index.php/admin/index/test to work.

Note: If your templates use the U() function, replace the fastcgi_split_path_info line with:

fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;

Solution 2 – Rewrite to hide the entry file (index.php): In the location / { ... } block, add the following rewrite rules:

if (!-e $request_filename) {

rewrite ^(.*)$ /index.php?s=$1 last;

break;

}

Save and restart Nginx. After this, Nginx will fully support PathInfo mode, and URLs like www.example.com/admin/index/test will resolve correctly.

PHPNginxserver configurationThinkPHPrewritePathInfo
php中文网 Courses
Written by

php中文网 Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.