lnmp1.3环境下配置pathinfo模式试了很多方法,都以失败告终,博主被这个问题困扰了很久,终于解决了!现记录如下:
1、打开php.ini
通常该文件在 /usr/local/php/etc/php.ini
vi /usr/local/php/etc/php.ini
找到 cgi.fix_pathinfo,默认为0,修改为1,保存退出;
2、打开nginx.conf配置文件
通常该文件在 /usr/local/nginx/conf/nginx.conf
vi /usr/local/nginx/conf/nginx.conf
(1)找到 include enable-php.conf;注释掉,在下面一行添加include enable-php-pathinfo.conf;
(2)添加以下代码,并保存退出1 #Rewrite模式 2 location / { 3 index index.htm index.html index.php; 4 if (!-e $request_filename) { 5 rewrite ^/(.*)$ /index.php/$1 last; 6 break; 7 } 8 } 9 #pathinfo模式10 location ~ \.php/?.*$ { 11 root /home/wwwroot/default;12 fastcgi_pass 127.0.0.1:9000;13 fastcgi_index index.php;14 include fastcgi.conf;15 #设置PATH_INFO并改写SCRIPT_FILENAME,SCRIPT_NAME服务器环境变量16 set $fastcgi_script_name2 $fastcgi_script_name;17 if ($fastcgi_script_name ~ "^(.+\.php)(/.+)$") {18 set $fastcgi_script_name2 $1;19 set $path_info $2;20 } 21 fastcgi_param PATH_INFO $path_info;22 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name2;23 fastcgi_param SCRIPT_NAME $fastcgi_script_name2;24 }
(3)重启nginx
service nginx restart
3、贴出完整server(截止此处已经成功,下面代码仅供参考)
server { listen 80; #listen [::]:80 default_server ipv6only=on; server_name www.lnmp.org; index index.html index.htm index.php; root /home/wwwroot/default; #error_page 404 /404.html; #include enable-php.conf; include enable-php-pathinfo.conf; #Rewrite模式 location / { index index.htm index.html index.php; #访问路径的文件不存在则重写URL转交给ThinkPHP处理 if (!-e $request_filename) { rewrite ^/(.*)$ /index.php/$1 last; break; } } #pathinfo模式 location ~ \.php/?.*$ { root /home/wwwroot/default; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; #加载Nginx默认"服务器环境变量"配置 include fastcgi.conf; #设置PATH_INFO并改写SCRIPT_FILENAME,SCRIPT_NAME服务器环境变量 set $fastcgi_script_name2 $fastcgi_script_name; if ($fastcgi_script_name ~ "^(.+\.php)(/.+)$") { set $fastcgi_script_name2 $1; set $path_info $2; } fastcgi_param PATH_INFO $path_info; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name2; fastcgi_param SCRIPT_NAME $fastcgi_script_name2; } location /nginx_status { stub_status on; access_log off; } location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$ { expires 30d; } location ~ .*\.(js|css)?$ { expires 12h; } access_log /home/wwwlogs/access.log; }