The URL mode of pathinfo and ThinkPHP is implemented under Nginx

  • 2020-05-13 04:32:04
  • OfStack

Open the Nginx configuration file (different versions of Nginx may be slightly different) and configure:


# To get rid of $ Is to not match the end of the line, that is, can match .php/ In order to achieve pathinfo
# If you don't need it php Suffixes, you can also get rid of them 
location ~ .php {
  # The original code 
  
  # Define variables  $path_info  , for storage pathinfo information 
  set $path_info "";
  # Define variables  $real_script_name Is used to store the real address 
  set $real_script_name $fastcgi_script_name;
  # If the address matches the regular expression inside the quotation marks 
  if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") {
      # Assigns the file address to a variable  $real_script_name
      set $real_script_name $1;
      # Assigns the parameter after the file address to a variable  $path_info
      set $path_info $2;
  }
  # configuration fastcgi the 1 Some parameters 
  fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
  fastcgi_param SCRIPT_NAME $real_script_name;
  fastcgi_param PATH_INFO $path_info;
}

This way, the Nginx server can support pathinfo. But if you want to support ThinkPHP with URL_MODE set to 2, you also need to configure the rewrite rule. Find the access_log statement and add the following statement above it:


# If the request is neither 1 It's not a file 1 Directory, then execute 1 Down rewrite rule 
if (!-e $request_filename) {
  # Address as the parameter rewrite to index.php On. 
  rewrite ^/(.*)$ /index.php/$1;
  # Use the following sentence if the subdirectory, will subdir Change the directory name. 
  #rewrite ^/subdir/(.*)$ /subdir/index.php/$1;
}

Save the configuration file, restart the Nginx service, and set URL_MODEL of ThinkPHP to 2. If it can be accessed normally, the configuration is successful.


Related articles: