The configuration of the Web-based general solution is as follows:
server {
...
Location/{
Index index.htm index.html index.php;
#访问路径的文件不存在则重写URL转交给ThinkPHP处理
if (!-e $request _filename) {
Rewrite ^/(. *) $/index.php/$1 last;
Break
}
}
Location ~ \.php/?. *$ {
Root/var/www/html/website;
Fastcgi_pass 127.0.0.1:9000;
Fastcgi_index index.php;
#加载Nginx默认 the server environment variables configuration
Include fastcgi.conf;
#设置PATH_INFO并改写SCRIPT_FILENAME, SCRIPT_NAME Server environment variables
Set $fastcgi _script_name2 $fastcgi _script_name;
if ($fastcgi _script_name ~ "^ (. +\.php) (/.+) $") {
Set $fastcgi _script_name2 $;
Set $path _info;
}
Fastcgi_param path_info $path _info;
Fastcgi_param script_filename $document _root$fastcgi_script_name2;
Fastcgi_param script_name $fastcgi _script_name2;
}
}
In fact, it should be simpler to use the FastCGI module with a fastcgi_split_path_info directive specifically designed to solve this kind of problem, which separates the URL according to the given regular expression, thus extracting the script name and path info information. Using this directive avoids using the IF statement, which is simpler to configure.
There is also a simpler way to determine if a file exists, using the try_files directive.
server {
...
Location/{
Index index.htm index.html index.php;
#如果文件不存在则尝试TP解析
Try_files $uri/index.php$uri;
}
Location ~. +\.php ($|/) {
Root/var/www/html/website;
Fastcgi_pass 127.0.0.1:9000;
Fastcgi_index index.php;
#设置PATH_INFO, note that Fastcgi_split_path_info has automatically rewritten the fastcgi_script_name variable,
#后面不需要再改写SCRIPT_FILENAME, script_name the environment variable, so you must set the fastcgi.conf before loading the
Fastcgi_split_path_info ^ (. +\.php) (/.*) $;
Fastcgi_param path_info $fastcgi _path_info;
#加载Nginx默认 the server environment variables configuration
Include fastcgi.conf;
}
}
The most perfect solution to Nginx deployment thinkphp Project