Configure pathinfo and ThinkPHP for URL Rewrite mode support under Nginx

  • 2020-05-09 19:58:01
  • OfStack

Open Nginx configuration file/usr local nginx/conf/nginx conf 1 is on the path, according to your installation path may change. If you have vhost configured and only need your vhost to support pathinfo, you can open your vhost configuration file directly. Find something like this (different versions of nginx may be slightly different, but not by much) :


    location ~ .*.(php|php5)?$
        {
                # The original code
        }

Modified to the following code:


    # 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 php5 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. However, if you want to support ThinkPHP's URL_MODE mode set to 2, you also need to configure the rewrite rule. Find the access_log statement and top it with the following statement:


    # 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;
        }

Finally, save the configuration file, restart nginx service, set URL_MODEL of ThinkPHP to 2, and visit your page. If you can access it normally, congratulations on the successful configuration of pathinfo ^_^


Related articles: