浏览 3.3k
如果在某个location里面定义一些指令,发现这些指令并不是按照他们生命和定义的顺序执行的。
那么他们执行的顺序是什么样子的? 是按照指定所在的模块所处的阶段以及模块在某一个阶段中模块的位置决定的吗?
比如如下的定义:
location /test {
directive A;
directive B;
directive C;
}
如果A,B指令所处的模块是content阶段,而且模块B位于模块A前面(只是假设), 指令C所处的模块是 rewrite阶段.
那么指令ABC的执行顺序就是 C , B , A 。
请问这样的理解对吗?

按点赞数排序
按时间排序
1、非脚本类的指令的顺序:1)先根据阶段,就像你说的;2)同一阶段内的模块之间,在configure时会决定顺序;
2、脚本类指令,如set、if等,是有顺序的,谁在前就先执行谁
1
回答于2021-03-05 13:49
根据你提供的配置和错误信息,有几个问题可能导致你无法访问多级目录的资源。下面是可能的解决方案:
1. 尝试一:访问/applet/img/user/fitness_logo2.png 提示404的配置:
```
location / {
root /app/docker/statics/web;
try_files $uri $uri/ /index.html;
index index.html index.htm;
}
location /applet {
root /app/docker/statics;
index index.html;
}
```
这个配置中,`location /applet` 指定了根目录为 `/app/docker/statics`,而你的资源实际路径是 `/app/docker/statics/applet`。因此,你需要在 `location /applet` 的配置中添加 `/applet` 部分,以便正确地指向资源路径。修改后的配置如下:
```
location / {
root /app/docker/statics/web;
try_files $uri $uri/ /index.html;
index index.html index.htm;
}
location /applet {
root /app/docker/statics;
index index.html;
try_files $uri $uri/ /applet$uri /applet$uri/ =404;
}
```
2. 尝试二:访问/applet/img/user/fitness_logo2.png 提示404的配置:
```
location / {
root /app/docker/statics/web;
try_files $uri $uri/ /index.html;
index index.html index.htm;
}
location /applet {
alias /app/docker/statics/applet;
}
```
在这个配置中,你使用了 `alias` 指令来映射 `/applet` 到 `/app/docker/statics/applet` 目录。但是,由于你的请求是 `/applet/img/user/fitness_logo2.png`,Nginx 尝试将请求 `/applet/img/user/fitness_logo2.png/` 解析为目录,并在该目录下寻找索引文件,导致出现错误。为了解决这个问题,你可以尝试在 `location /applet` 的配置中使用 `try_files` 指令来显式指定文件的匹配。修改后的配置如下:
```
location / {
root /app/docker/statics/web;
try_files $uri $uri/ /index.html;
index index.html index.htm;
}
location /applet {
alias /app/docker/statics/applet;
try_files $uri $uri/ =404;
}
```
微信公众号
加入微信群