A-A+
Nginx服务器下使Thinkphp URL模式支持PATHINFO模式和REWRITE模式
默认nginx服务器是不识别pathinfo模式和rewrite模式的路由的,下面我们做一些配置,让nginx服务器支持这些路由模式
PATHINFO
- 找到location ~ \.php {
- #\.php$ 里面的$去掉,并在里面加上下面两句
- fastcgi_split_path_info ^(.+\.php)(.*)$; #增加
- fastcgi_param PATH_INFO $fastcgi_path_info; #增加
加好之后大致是这个样子的
- location ~ \.php {
- root html
- fastcgi_pass 127.0.0.1:9000;
- fastcgi_index index.php;
- fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
- include fastcgi_params;
- fastcgi_split_path_info ^(.+\.php)(.*)$; #添加
- fastcgi_param PATH_INFO $fastcgi_path_info; #添加
- }
REWRITE去掉index.php
找到location / { 在里面加上
- if (!-e $request_filename) {
- rewrite ^/(.*)$ /index.php?s=$1 last;
- break;
- }
加好之后是这样子的
- location / {
- root html
- index index.html index.htm index.php;
- if (!-e $request_filename) { #添加
- rewrite ^/(.*)$ /index.php?s=$1 last; #添加
- break; #添加
- } #添加
- }
如果thinkphp不是部署在网站根目录下,即需要把
- rewrite ^/(.*)$ /index.php?s=$1 last;
- 改成
- rewrite ^/子目录/(.*)$ /子目录/index.php?s=$1 last;
如果根目录下有多个项目的话,可以写多个rewrite:
- if (!-e $request_filename) {
- rewrite ^/项目1/(.*)$ /项目1/index.php?s=$1 last;
- rewrite ^/项目2/(.*)$ /项目2/index.php?s=$1 last;
- rewrite ^/项目3/(.*)$ /项目3/index.php?s=$1 last;
- break;
- }