A-A+

Nginx服务器下使Thinkphp URL模式支持PATHINFO模式和REWRITE模式

2020年12月12日 PHP开源系统 暂无评论 阅读 0 views 次

默认nginx服务器是不识别pathinfo模式和rewrite模式的路由的,下面我们做一些配置,让nginx服务器支持这些路由模式

PATHINFO

  1. 找到location ~ \.php {     
  2.   
  3. #\.php$ 里面的$去掉,并在里面加上下面两句  
  4.   
  5. fastcgi_split_path_info ^(.+\.php)(.*)$;     #增加  
  6. fastcgi_param PATH_INFO $fastcgi_path_info;    #增加  

加好之后大致是这个样子的

  1. location ~ \.php {  
  2.             root       html  
  3.             fastcgi_pass   127.0.0.1:9000;  
  4.             fastcgi_index  index.php;  
  5.             fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;  
  6.             include        fastcgi_params;  
  7.             fastcgi_split_path_info ^(.+\.php)(.*)$;     #添加  
  8.             fastcgi_param PATH_INFO $fastcgi_path_info;    #添加  
  9.         }  

REWRITE去掉index.php

找到location / { 在里面加上

  1. if (!-e $request_filename) {  
  2.    rewrite  ^/(.*)$ /index.php?s=$1  last;  
  3.    break;  
  4. }  

加好之后是这样子的

  1. location / {  
  2.             root       html   
  3.             index  index.html index.htm index.php;  
  4.   
  5.             if (!-e $request_filename) {                 #添加  
  6.                    rewrite  ^/(.*)$ /index.php?s=$1  last; #添加  
  7.                    break;                                 #添加  
  8.             }                                             #添加  
  9.         }  

如果thinkphp不是部署在网站根目录下,即需要把

  1. rewrite  ^/(.*)$ /index.php?s=$1  last;  
  2.   
  3. 改成  
  4.   
  5. rewrite  ^/子目录/(.*)$ /子目录/index.php?s=$1  last;  

如果根目录下有多个项目的话,可以写多个rewrite:

  1. if (!-e $request_filename) {  
  2.      rewrite  ^/项目1/(.*)$ /项目1/index.php?s=$1  last;  
  3.      rewrite  ^/项目2/(.*)$ /项目2/index.php?s=$1  last;  
  4.      rewrite  ^/项目3/(.*)$ /项目3/index.php?s=$1  last;  
  5.      break;  
  6. }  
标签:

给我留言