网站首页 > 技术教程 正文
由于应用需求,这个 r 目录需要单独拉出来做处理,nginx 最开始是这样写的:
 server {
 root /home/webadm/htdocs;
 index index.php;
 location /r/ {
 root /diska/htdocs;
 }
 location ~ \.php {
 fastcgi_pass unix:/home/php/php-fastcgi.sock;
 fastcgi_index index.php;
 include fastcgi_params;
 expires off;
 }
 }
最开始的写法是这样的,由于代码从原来的/home/webadm/htdocs/r 拷贝到 /diska/htdocs/r,所以未发现问题。
代码更新后,发现访问r 下面的 php 相关文件直接 404.
看了一下官方的文档,我们先来理解
 location /r/ {
 ...
 }
# matches any query beginning with /r/ and continues searching,
# so regular expressions will be checked. This will be matched only if
# regular expressions don't find a match.
也就是说当请求的是 /r/example.php 时,nginx 匹配location 后继续查找,最后匹配到 location ~\.php .这时候
nginx 解释文件的路径在 /home/webadm/htdocs/r/example.php。由于之前的文件本来在那里,所以没发现问题。
那么我们再来看看这个location 的解释:
 location ~ /r/ {
 ...
 }
# matches any query beginning with /r/ and halts searching,
# so regular expressions will not be checked.
也就是说找到匹配的location r 之后,nginx 将不往下面的 locatioin 处理了。如果将最上面的 location r 换成这
样的配置,那么php 文件将会被浏览器直接下载:
 location ~ /r/ {
 root /diska/htdocs;
 }
nginx 不支持全局的fastcgi 设定(类似于访问日志,错误日志这样)的配置,所以处理的方法有二:
1. 在 location ~/r/ 中增加fastcgi 的处理。或者增加一个子location 匹配php 的,具体如下:
 location ~ /r/ {
 root /diska/htdocs;
 fastcgi_pass unix:/home/php/php-fastcgi.sock; #这里也可能是tcp 
 include fastcgi_params;
 }
或者:
 location /r/ {
 root /diska/htdocs;
 }
 location ~ /r/.+\.php {
 fastcgi_pass unix:/home/php/php-fastcgi.sock; #这里也可能是tcp 
 include fastcgi_params;
 }
2. 在全局的php 配置中处理该 location:
 location /r/ {
 root /diska/htdocs;
 }
 location ~ \.php {
 if ($uri ~ /r/) { #这里判断是r 目录过来的匹配的php,重新指定其root 目录
 root /diska/htdocs;
 }
 fastcgi_pass unix:/home/php/php-fastcgi.sock;
 fastcgi_index index.php;
 include fastcgi_params;
 expires off;
 }
另外这里需要注意的是,前面的 location /r/ 不能是 location ~/r/ ,否则php 文件会直接下载。
猜你喜欢
- 2024-10-10 译荐|Nginx: 高性能与规模化的设计逻辑
 - 2024-10-10 终于有人把「编程软件安装和环境配置」的教程整理成了一个文档
 - 2024-10-10 破解 非root用户禁止使用1024以下端口限制
 - 2024-10-10 Nginx反向代理(nginx反向代理 传递客户端ip)
 - 2024-10-10 在Linux中通过expect工具实现脚本的自动交互
 - 2024-10-10 我在自己腾讯云服务器上更改nginx配置的操作指引
 - 2024-10-10 NGINX内部工作原理:我们如何设计性能和规模
 - 2024-09-11 Linux下玩转nginx系列(六)---nginx实现cache(缓存)服务
 - 2024-09-11 Nginx从理论到实践超详细笔记(nginx 原理与架构)
 - 2024-09-11 还在为Root用户启动程序的安全警告而烦恼吗?试试setcap
 
欢迎 你 发表评论:
- 10-23Excel计算工龄和年份之差_excel算工龄的公式year
 - 10-23Excel YEARFRAC函数:时间的"年份比例尺"详解
 - 10-23最常用的10个Excel函数,中文解读,动图演示,易学易用
 - 10-23EXCEL中如何计算截止到今日(两个时间中)的时间
 - 10-2390%人不知道的Excel神技:DATEDIF 精准计算年龄,告别手动算错!
 - 10-23计算工龄及工龄工资(90%的人搞错了):DATE、DATEDIF组合应用
 - 10-23Excel中如何计算工作日天数?用这两个函数轻松计算,附新年日历
 - 10-23怎样快速提取单元格中的出生日期?用「Ctrl+E」批量搞定
 
- 最近发表
 - 
- Excel计算工龄和年份之差_excel算工龄的公式year
 - Excel YEARFRAC函数:时间的"年份比例尺"详解
 - 最常用的10个Excel函数,中文解读,动图演示,易学易用
 - EXCEL中如何计算截止到今日(两个时间中)的时间
 - 90%人不知道的Excel神技:DATEDIF 精准计算年龄,告别手动算错!
 - 计算工龄及工龄工资(90%的人搞错了):DATE、DATEDIF组合应用
 - Excel中如何计算工作日天数?用这两个函数轻松计算,附新年日历
 - 怎样快速提取单元格中的出生日期?用「Ctrl+E」批量搞定
 - Excel日期函数之DATEDIF函数_excel函数datedif在哪里
 - Excel函数-DATEDIF求司龄_exceldatedif函数计算年龄
 
 
- 标签列表
 - 
- 下划线是什么 (87)
 - 精美网站 (58)
 - qq登录界面 (90)
 - nginx 命令 (82)
 - nginx .http (73)
 - nginx lua (70)
 - nginx 重定向 (68)
 - Nginx超时 (65)
 - nginx 监控 (57)
 - odbc (59)
 - rar密码破解工具 (62)
 - annotation (71)
 - 红黑树 (57)
 - 智力题 (62)
 - php空间申请 (61)
 - 按键精灵 注册码 (69)
 - 软件测试报告 (59)
 - ntcreatefile (64)
 - 闪动文字 (56)
 - guid (66)
 - abap (63)
 - mpeg 2 (65)
 - column (63)
 - dreamweaver教程 (57)
 - excel行列转换 (56)
 
 

本文暂时没有评论,来添加一个吧(●'◡'●)