概述
在上一篇文章Nginx配置Thinkphp支持URL Rewrite中已經(jīng)介紹了如何配置Nginx支持ThinkPHP的URL Rewrite,但是上文針對(duì)的是Centos平臺(tái),這次因?yàn)槟承┨厥獾脑颍?wù)器環(huán)境必須用ubuntu,本來以為和Cetons中一模一樣,但是配置完了發(fā)現(xiàn)不能使用,所以就百度了一些文章。
配置方法
TP官方解決方案
location ~ .php
{
#原有代碼
#定義變量 $path_info ,用于存放pathinfo信息
set $path_info "";
#定義變量 $real_script_name,用于存放真實(shí)地址
set $real_script_name $fastcgi_script_name;
#如果地址與引號(hào)內(nèi)的正則表達(dá)式匹配
if ($fastcgi_script_name ~ "^(.+?\.php)(/.+)$") {
#將文件地址賦值給變量 $real_script_name
set $real_script_name $1;
#將文件地址后的參數(shù)賦值給變量 $path_info
set $path_info $2;
}
#配置fastcgi的一些參數(shù)
fastcgi_param SCRIPT_FILENAME $document_root$real_script_name;
fastcgi_param SCRIPT_NAME $real_script_name;
fastcgi_param PATH_INFO $path_info;
}
這樣,nginx服務(wù)器就可以支持pathinfo了。但是如果要支持ThinkPHP的URL_MODE設(shè)置為2的模式,還需要配置rewrite規(guī)則。找到access_log語句,在其上方加上以下語句:
#如果請(qǐng)求既不是一個(gè)文件,也不是一個(gè)目錄,則執(zhí)行一下重寫規(guī)則
if (!-e $request_filename)
{
#地址作為將參數(shù)rewrite到index.php上。
rewrite ^/(.*)$ /index.php/$1;
#若是子目錄則使用下面這句,將subdir改成目錄名稱即可。
#rewrite ^/subdir/(.*)$ /subdir/index.php/$1;
}
網(wǎng)友解決方案
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;
}
}
然后在localhost ~ .php{}配置欄目中添加如下兩行:
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
完整配置如下:
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;
}