Configure pathinfo mode in nginx to support URL rewrite of thinkphp

  • 2020-05-07 20:51:56
  • OfStack

ThinkPHP was used as the development framework in a recent project. On URL, we used PATHINFO mode, but Nginx does not support PATHINFO by default, which requires manual configuration. Therefore, we configured Nginx's PATHINFO support in the following ways:
Modify nginx.conf, find location ~.php ${} in server, change to location ~.php {}, and add the following:


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;

However, no matter how we tested it, we found that PATHINFO mode always failed to run correctly. Even after running, the {:U} method always failed. After printing $_SERVER, we found that PHP_SELF was always wrong.
However, PATHINFO still failed to work properly, so we determined that it was the configuration problem of ThinkPHP, and found that it was actually the _PHP_FILE problem of ThinkPHP. We added the configuration define('_PHP_FILE_',$_SERVER['SCRIPT_NAME'] in the entry file index.php. Problem solving.
If the $_SERVER['SCRIPT_NAME'] path is also wrong, replace it with the correct file path first.


Related articles: