nginx、TP框架实现兼容pathinfo和rewrite两种url访问方式

释放双眼,带上耳机,听听看~!

转载自:http://blog.csdn.net/jo_andy/article/details/52598097

环境:centos7,yum安装的nginx1.10、php-fpm,tp3.2 
本方法只需要配置nginx.conf的一个文件就可以支持pathinfo和rewrite两种url访问方式


1
2
1vim /etc/nginx/nginx.conf
2
  • 1

1、支持rewrite方式: 
在 
location / 处添加以下代码


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

最终变成


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

2.实现pathinfo模式 
找到有效的 
location ~ .php$那部分 
首先,将这个
$去掉, 
然后里面添加以下两行代码


1
2
3
1fastcgi_split_path_info ^(.+\.php)(.*)$;
2fastcgi_param PATH_INFO $fastcgi_path_info;
3
  • 1
  • 2

最终变成


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

3.重启nginx和php-fpm即可使用了


1
2
3
1service nginx restart
2service php-fpm restart
3

给TA打赏
共{{data.count}}人
人已打赏
安全运维

WordPress网站专用docker容器环境带Waf

2020-7-18 20:04:44

安全运维

运维安全-Gitlab管理员权限安全思考

2021-9-19 9:16:14

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索