Under Ubuntu Nginx configures Pathinfo and URl Rewrite modes for ThinkPHP

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

An overview of the

In article 1 Nginx configuration Thinkphp support URL Rewrite have introduced how to configure the Nginx support ThinkPHP URL Rewrite, but the above is for Centos platform, this time for some special reason, must use ubuntu server environment, thought and Cetons mode 1 sample, but finished configuration found that can't use, so baidu 1 some articles.

Configuration method
TP official solution


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

E-friend solution

location / {
                root /var/www;
                # First attempt to serve request as file, then
                # as directory, then fall back to index.html
                try_files $uri $uri/ /index.html;
                # Uncomment to enable naxsi on this location
                # include /etc/nginx/naxsi.rules
                if (!-e $request_filename)
                {
                        rewrite ^/PHPParser/(.*)$ /PHPParser/index.php?s=$1 last;
                        break;
                }
        }

Then, in the localhost ~.php {} configuration column, add the following two lines:


fastcgi_split_path_info ^(.+\.php)(.*)$;                            
fastcgi_param PATH_INFO $fastcgi_path_info;

The complete configuration is as follows:

location ~ \.php$ {
                root /var/www;
                try_files $uri = 404;
                fastcgi_split_path_info ^(.+\.php)(/.+)$;
        #       # NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
        #
        #       # With php5-cgi alone:
                #fastcgi_pass 127.0.0.1:9000;
                fastcgi_split_path_info ^(.+\.php)(.*)$;
                fastcgi_param PATH_INFO $fastcgi_path_info;
        #       # With php5-fpm:
                fastcgi_pass unix:/var/run/php5-fpm.sock;
                fastcgi_index index.php;
                include fastcgi_params;
        }


Related articles: