天天看点

TP环境配置之 lnmp 配置thinkphp路由重写的代码

使用更简单的方法,fastcgi模块自带了一个fastcgi_split_path_info指令专门用来解决此类问题的,该指令会根据给定的正则表达式来分隔URL,从而提取出脚本名和path info信息,使用这个指令可以避免使用if语句,配置更简单。

另外判断文件是否存在也有更简单的方法,使用try_files指令即可。 [plain]  view plain  copy

  1. server {  
  2.  ...  
  3.     location / {  
  4.         index  index.htm index.html index.php;  
  5.         #如果文件不存在则尝试TP解析  
  6.         try_files  $uri  /index.php$uri;  
  7.     }  
  8.     location ~ .+\.php($|/) {  
  9.         root        /var/www/html/website;  
  10.         fastcgi_pass   127.0.0.1:9000;  
  11.         fastcgi_index  index.php;  
  12.         #设置PATH_INFO,注意fastcgi_split_path_info已经自动改写了fastcgi_script_name变量,  
  13.         #后面不需要再改写SCRIPT_FILENAME,SCRIPT_NAME环境变量,所以必须在加载fastcgi.conf之前设置  
  14.         fastcgi_split_path_info  ^(.+\.php)(/.*)$;  
  15.         fastcgi_param  PATH_INFO $fastcgi_path_info;  
  16.         #加载Nginx默认"服务器环境变量"配置  
  17.         include        fastcgi.conf;  
  18.     }  
  19. }  

继续阅读