Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nginx 配置说明 #55

Open
niexia opened this issue Jun 30, 2020 · 0 comments
Open

nginx 配置说明 #55

niexia opened this issue Jun 30, 2020 · 0 comments
Labels
service service knowledge

Comments

@niexia
Copy link
Owner

niexia commented Jun 30, 2020

try_files

try_files 是 nginx 中 http_core 核心模块所带的指令,主要是能替代一些 rewrite 的指令,提高解析效率。

语法规则

  • 格式1:try_files file ... uri;
  • 格式2:try_files file ... = code;

含义

Checks the existence of files in the specified order and uses the first found file for request processing; the processing is performed in the current context. The path to a file is constructed from the fileparameter according to the root and alias directives. It is possible to check directory’s existence by specifying a slash at the end of a name, e.g. “$uri/”. If none of the files were found, an internal redirect to the uri specified in the last parameter is made.

  1. 按指定的 file 顺序查找存在的文件,并使用第一个找到的文件进行请求处理
  2. 查找路径是按照给定的 rootalias 为根路径来查找的
  3. 如果给出的 file 都没有匹配到,则重新请求最后一个参数给定的 uri,就是新的 location 匹配
  4. 如果是格式 2,如果最后一个参数是 = 404 ,若给出的 file 都没有匹配到,则最后返回 404 的响应码

例子

location /images/ {
  root /opt/html/;
  try_files $uri $uri/ /images/default.gif; 
}

比如请求 127.0.0.1/images/test.gif 会依次查找

  1. 文件 /opt/html/images/test.gif
  2. 文件夹 /opt/html/images/test.gif/ 下的 index 文件
  3. 请求 127.0.0.1/images/default.gif
  4. 其他注意事项:try-files 如果不写上 $uri/,当直接访问一个目录路径时,并不会去匹配目录下的索引页,即访问 127.0.0.1/images/ 不会去访问 127.0.0.1/images/index.html
@niexia niexia added the service service knowledge label Jun 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
service service knowledge
Projects
None yet
Development

No branches or pull requests

1 participant