Nginx重写URL配置PHP多入口

0. 前提假设

微信号:buddha2080

1. 问题描述

PHP项目有多个入口文件,如入口文件:index.php,admin.php,app.php,api.php

常规访问如下:
http://www.domain.com/index.php/xxx/xxx/...
http://www.domain.com/admin.php/xxx/xxx/...
http://www.domain.com/app.php/xxx/xxx/...
http://www.domain.com/api.php/xxx/xxx/...

不想让用户从URL地址中,是用什么语言开发的,则需要隐藏.php,最终如下:

http://www.domain.com/index/xxx/xxx/...
http://www.domain.com/admin/xxx/xxx/...
http://www.domain.com/app/xxx/xxx/...
http://www.domain.com/api/xxx/xxx/...

2. 解决问题的Nginx配置

server {
	listen 80;
	server_name caorulai.com;
	root /data/www;
	index index.php index.html index.htm;

	#隐藏 .php
	location / {
		if (!-e $request_filename) {
		
			# admin.php
			rewrite ^/admin/(.*)$ /admin.php/$1 last;
			
			# app.php
			# rewrite ^/app/(.*)$ /app.php/$1 last;
			
			# api.php
			# rewrite ^/api/(.*)$ /api.php/$1 last;
			
			# 前面的url过滤之后,再走index.php入口
			rewrite ^/(.*)$ /index.php/$1 last;
		}   
	}

	#pathinfo 模式
	location ~ \.php($|/) {
		fastcgi_pass   127.0.0.1:9000;
		fastcgi_index  index.php;
		fastcgi_split_path_info ^(.+\.php)(.*)$;
		fastcgi_param   PATH_INFO $fastcgi_path_info;
		fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
		include        fastcgi_params;
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序员buddha

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值