Rootop 服务器运维与web架构

nginx中try_files作用及location @作用

# nginx 配置文件:

server {

    listen 80;
    server_name  api.xxx.com;

    root /mnt/try;

    location / {
        add_header  Content-Type 'text/html; charset=utf-8';
        #echo $uri;
        try_files $uri @default;
    }

    location @default {
        root /mnt/default;
    }
}

location @xxx解释:定义一个location段,不能被外部请求所访问,只能用于nginx内部配置指令使用,比如 try_files、error_page。
$uri解释:URI代表资源的名称

浏览器访问 http://api.xxx.com/abc/index.html 时,当前的$uri值为/abc/index.html

可以通过编译nginx参数--add-module添加第三方echo模块打印$uri

echo模块地址:https://github.com/openresty/echo-nginx-module/archive/v0.61.tar.gz

# try_files作用:
try_files会先尝试去/mnt/try目录下找abc目录下的index.html,如果有,直接返回,没有的话则跳转到@default部分(内部重定向)。
在default部分会去/mnt/default目录下找abc目录下的index.html,有,直接返回,没有就返回404错误。

try_files可以理解为实现rewrite的作用。

原创文章,转载请注明。本文链接地址: https://www.rootop.org/pages/4343.html

作者:Venus

服务器运维与性能优化

评论已关闭。