1. 程式人生 > 實用技巧 >淺談thinkphp的nginx配置,以及重寫隱藏index.php入口檔案方法

淺談thinkphp的nginx配置,以及重寫隱藏index.php入口檔案方法

1,心血來潮,把ThinkPHP專案部署到了nginx上,以上是在apache上跑的。突然發現nginx不支援pathinfo功能,難怪在TP中調怎麼都沒管用。

2,開始上檔案了,比網上其他一些雜的好多了:

server { 
listen 80; 
#listen [::]:80; 
server_name www.tp.com tp.com; 
index index.html index.htm index.php default.html default.htm default.php; 
root /home/wwwroot/www.tp.com; 
include index.php.conf; 
#error_page 404 /404.html; 
#include enable-php.conf; 
include enable-php-pathinfo.conf; ##這個地方需要說明下:我用的是lnmp一鍵安裝包,可能這個pathinfo.conf配置檔名有些不一樣, 
## 有檔名為enable-php.conf,也有enable-php-pathinfo.conf 
## 目錄在/usr/local/nginx/conf 可以自己去看看,帶有pathinfo 
#error_page 404 /404.html

 location /app/ {  #因為我的專案入口檔案是放到app目錄中的(app目錄與Think目錄同級),這樣實現了隱藏index.php功能         
  if (!-e $request_filename) {  
  rewrite ^/app/(.*)$ /app/index.php/$1 last;
  break;
  }
 }
 location ~ ^(.+\.php)(.*) {
try_files $uri =404; 
fastcgi_pass 127.0.0.1:9000; 
fastcgi_pass unix:/run/php5-fpm.sock; 
fastcgi_index index.php; 
include fastcgi_params; 
# include fcgi.conf;

set $real_script_name $fastcgi_script_name; 
set $path_info “”; 
if ($fastcgi_script_name ~ “^(.+?.php)(/.+)$”){ 
set $real_script_name $1; 
set $path_info $2; 
} 
fastcgi_param SCRIPT_FILENAME $document_root 
$real_script_name; 
fastcgi_param SCRIPT_NAME $real_script_name; 
fastcgi_param PATH_INFO $path_info; 
} 
access_log /home/wwwlogs/www.tp.com.log; 
}

直接上我的配置檔案截圖吧:

我的目錄結構

看,現在可以支援以下路由了,pathinfo以及rewrite隱藏index.php入口檔案

以上這篇淺談thinkphp的nginx配置,以及重寫隱藏index.php入口檔案方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援碼農教程。