在Nginx下运行thinkphp

本文详细介绍了Nginx的rewrite规则配置方法,包括如何隐藏入口文件、重定向及缓存设置等实用技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先记录个大概


根据手册上所说,可以配置nginx的rewrite规则,实现正常运行以及隐藏index.php这类的入口文件。它是这么写的:

location / { // …..省略部分代码
    if (!-e $request_filename) {
        rewrite  ^(.*)$  /index.php?s=$1  last;
        break;
    }
 }
所以,这里也可以看出,其实apache下,入口文件后面的就是参数$_GET['s']。并且这么访问的确是对的。


然后关于Nginx的重写规则,要进行简单的配置其实不难,还是拿上面的来说,rewrite关键字后面就是一个正则,去匹配请求的url,如果匹配,就用后面的进行代替,其中$1是指前面正则里面的子串,括号里的,所以如果有多个字串,还会有$2、$3等;


对了,前面的 if (!-e $request_filename) 是指找不到文件。


然后,摘抄了几个示例,的确非常有用:

1.当访问的文件和目录不存在时,重定向到某个php文件
if( !-e $request_filename )
{
    rewrite ^/(.*)$ index.php last;
}
 
2.目录对换 /123456/xxxx  ====>   /xxxx?id=123456
rewrite ^/(\d+)/(.+)/  /$2?id=$1 last;
 
3.如果客户端使用的是IE浏览器,则重定向到/ie目录下
if( $http_user_agent  ~ MSIE)
{
    rewrite ^(.*)$ /ie/$1 break;
}
 
4.禁止访问多个目录
location ~ ^/(cron|templates)/
{
    deny all;
    break;
}
 
5.禁止访问以/data开头的文件
location ~ ^/data
{
    deny all;
}
 
6.禁止访问以.sh,.flv,.mp3为文件后缀名的文件
location ~ .*\.(sh|flv|mp3)$
{
    return 403;
}
 
7.设置某些类型文件的浏览器缓存时间
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
    expires 30d;
}
location ~ .*\.(js|css)$
{
    expires 1h;
}


关于last和break:

使用last和break实现URI重写,浏览器地址栏不变。而且两者有细微差别,使用alias指令必须用last标记;使用proxy_pass指令时,需要使用break标记。Last标记在本条rewrite规则执行完毕后,会对其所在server{......}标签重新发起请求,而break标记则在本条规则匹配完成后,终止匹配。

一般在跟location中(location /{...})或直接在server标签中编写rewrite规则,推荐使用last标记;在非根location中(location /cms/{...}),则使用break。

如果URI中含有参数(/app/test.php?id=5),默认情况下参数会被自动附加到替换串上,你可以通过在替换串的末尾加上?标记来解决这一问题。from here

Rebuild started: Project: Project *** Using Compiler 'V6.22', folder: 'E:\Keil_v5\ARM\ARMCLANG\Bin' Rebuild target 'Target 1' assembling startup_stm32f10x_md.s... Start/core_cm3.c(445): error: non-ASM statement in naked function is not supported 445 | uint32_t result=0; | ^ Start/core_cm3.c(442): note: attribute is here 442 | uint32_t __get_PSP(void) __attribute__( ( naked ) ); | ^ Start/core_cm3.c(465): error: parameter references not allowed in naked functions 465 | "BX lr \n\t" : : "r" (topOfProcStack) ); | ^ Start/core_cm3.c(461): note: attribute is here 461 | void __set_PSP(uint32_t topOfProcStack) __attribute__( ( naked ) ); | ^ Start/core_cm3.c(479): error: non-ASM statement in naked function is not supported 479 | uint32_t result=0; | ^ Start/core_cm3.c(476): note: attribute is here 476 | uint32_t __get_MSP(void) __attribute__( ( naked ) ); | ^ Start/core_cm3.c(499): error: parameter references not allowed in naked functions 499 | "BX lr \n\t" : : "r" (topOfMainStack) ); | ^ Start/core_cm3.c(495): note: attribute is here 495 | void __set_MSP(uint32_t topOfMainStack) __attribute__( ( naked ) ); | ^ 4 errors generated. compiling core_cm3.c... compiling misc.c... compiling system_stm32f10x.c... compiling stm32f10x_adc.c... compiling stm32f10x_dac.c... compiling stm32f10x_exti.c... compiling stm32f10x_dbgmcu.c... compiling stm32f10x_dma.c... compiling stm32f10x_crc.c... compiling stm32f10x_cec.c... compiling stm32f10x_bkp.c... compiling stm32f10x_can.c... compiling stm32f10x_flash.c... compiling stm32f10x_pwr.c... compiling stm32f10x_fsmc.c... compiling stm32f10x_
03-31
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值