当前位置:首页 > 技术文章 > 正文内容

nginx location 多root理解location

arlanguage5个月前 (12-14)技术文章42

由于应用需求,这个 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 文件会直接下载。

扫描二维码推送至手机访问。

版权声明:本文由AR编程网发布,如需转载请注明出处。

本文链接:http://www.arlanguage.com/post/167.html

标签: php 5.6 nginx
分享给朋友:

“nginx location 多root理解location” 的相关文章

windows下的nginx安装和使用

.1 去官网下载相应的安装包:http://nginx.org/en/download.html1.2 解压后进入PowerShell(按住“shift”+“鼠标右键”)窗口,进入到nginx目录,输入start nginx.exe 进行nginx的安装安装成功后,在“任务管理器”中会显示“ngin...

nginx知识梳理及配置详解

nginx安装#nginx安装 yum -y install gcc pcre-devel openssl-devel #依赖包 useradd -s /sbin/nologin nginx ./configure --prefix=/usr/local/nginx #...

5分钟搞懂nginx的location匹配规则

目录概述location介绍location指令语法location配置实例解析location常用场景实战禁止访问.sh后缀的文件实例php7进阶到架构师相关阅读概述这是关于php进阶到架构之Nginx进阶学习的第一篇文章:5分钟搞懂nginx的location匹配规则第一篇:5分钟搞懂nginx...

Linux 网络延迟排查方法 linux延迟10秒

在 Linux 服务器中,可以通过内核调优、DPDK 以及 XDP 等多种方式提高服务器的抗攻击能力,降低 DDoS 对正常服务的影响。在应用程序中,可以使用各级缓存、WAF、CDN 等来缓解 DDoS 对应用程序的影响。但是需要注意的是,如果 DDoS 流量已经到达 Linux 服务器,那么即使应...

Nginx安全相关配置常用教程 nginx安全策略

1. 限制请求速度设置 Nginx、Nginx Plus 的连接请求在一个真实用户请求的合理范围内。比如,如果你觉得一个正常用户每两秒可以请求一次登录页面,你就可以设置 Nginx 每两秒钟接收一个客户端 IP 的请求(大约等同于每分钟30个请求)。limit_req_zone $binary_re...

宝塔面板如何关闭https强制跳转http/https共存

宝塔面板如何关闭https强制跳转http/https共存在 宝塔面板 中,如果你需要关闭 HTTPS 强制跳转并实现 HTTP 和 HTTPS 共存,可以通过以下步骤完成配置:一、关闭 HTTPS 强制跳转登录宝塔面板进入宝塔后台,点击左侧菜单中的 网站。找到目标网站在网站列表中找到需要取消 HT...