Download Nginx So fire, whether you also adopt LNMP architecture. Then in the Nginx How to configure, in order to support the thinkphp pathinfo routing mode, the following is the example of configuration, the domain name and the path to their actual situation is good.
The configuration is as follows:
server {
Listen 80;
server_name www.111cn.net;
Root/www/daixiaorui;
Location/{
Index index.htm index.html index.php;
#如果文件不存在则尝试TP解析
Try_files $uri/index.php$uri;
}
Location ~. +\.php ($|/) {
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 environment variables, so you must set the fastcgi.conf before loading the
Fastcgi_split_path_info ^ (. +\.php) (/.*) $;
Fastcgi_param path_info $fastcgi _path_info;
#加载Nginx默认 "Server environment variables" configuration
Include fastcgi.conf;
}
}
Remember to restart the Nginx service when the configuration is complete.
The following describes how to enable Nginx to support thinkphp pathinfo and URL rewrite patterns.
1, Thinkphp gave the thinkphp official solution, as follows:
Open the Nginx profile/ETC/NGINX/NGINX.COF is generally in this path, depending on your installation path may vary. If you configure Vhost, and you only need one of your vhost support PathInfo, you can open your Vhost configuration file directly. Find a code similar to the following (different versions of Nginx may be slightly different, but not very far apart):
Location ~. php
{
#原有代码
#定义变量 $path _info, for storing pathinfo information
Set $path _info "";
#定义变量 $real _script_name, for storing real addresses
Set $real _script_name $fastcgi _script_name;
#如果地址与引号内的正则表达式匹配
if ($fastcgi _script_name ~ "^ (. +?\.php) (/.+) $") {
#将文件地址赋值给变量 $real _script_name
Set $real _script_name $;
#将文件地址后的参数赋值给变量 $path _info
Set $path _info $;
}
#配置fastcgi的一些参数
Fastcgi_param script_filename $document _root$real_script_name;
Fastcgi_param script_name $real _script_name;
Fastcgi_param path_info $path _info;
}
In this way, the Nginx server can support PathInfo. However, you also need to configure the rewrite rule if you want to support the thinkphp Url_mode set to 2 mode. Locate the Access_log statement, and add the following statement above it:
#如果请求既不是一个文件, not a directory, then execute the rewrite rule
if (!-e $request _filename)
{
#地址作为将参数rewrite到index. php.
Rewrite ^/(. *) $/index.php/$1;
#若是子目录则使用下面这句, change the subdir to the directory name.
#rewrite ^/subdir/(. *) $/subdir/index.php/$1;
}
Although the above method is thinkphp official to give, presumably also be verified, but the sad urge is to me not to work.
2, My Solution
I was configured under sites (Vhost), under the/etc/nginx/sites-available/directory. Of course, you can also directly in the/etc/nginx/nginx.conf configuration.
Add the following code in the localhost/{} configuration:
#如果请求既不是一个文件, not a directory, then execute the rewrite rule
if (!-e $request _filename)
{
#地址作为将参数rewrite到index. php.
Rewrite ^/(. *) $/index.php/$1;
#若是子目录则使用下面这句, change the subdir to the directory name.
#rewrite ^/subdir/(. *) $/subdir/index.php/$1;
}
The complete code is as follows:
Location/{
root/var/www;
# 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 add the following two lines to the localhost ~ \.php{} configuration column:
Fastcgi_split_path_info ^ (. +\.php) (. *) $;
Fastcgi_param path_info $fastcgi _path_info;
The full 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;
}