zoukankan      html  css  js  c++  java
  • Nginx alias root 和 try_files 基本使用


    请求都用域名 test.com

    root

    http://nginx.org/en/docs/http/ngx_http_core_module.html#root

    语法

    Syntax:	root path;
    Default:	
    root html;
    Context:	http, server, location, if in location
    
    

    一般配置

    location /i/ {
        root /data/w3;
    }
    

    请求 /i/top.gif 是找到文件 data/w3/i/top.gif

    root 实现的是url 拼接。

    location /mainland/pgc/ {
    	root /tmp/pgc/pgc/;
    }
    

    请求 http://test.com/mainland/pgc/ 实际目录为 /tmp/pgc/pgc/mainland/pgc/index.html

    alias

    http://nginx.org/en/docs/http/ngx_http_core_module.html#alias

    语法

    Syntax:	alias path;
    Default:	—
    Context:	location
    
    

    一般配置

    location /i/ {
        alias /data/w3/images/;
    }
    

    请求 /i/top.gif 是找到文件 /data/w3/images/top.gif

    注意: alias 末尾需要增加 /

    增加正则

    官网文档

    if alias is used inside a location defined with a regular expression then such regular expression should contain captures and alias should refer to these captures (0.7.40), for example:
    
    location ~ ^/users/(.+.(?:gif|jpe?g|png))$ {
        alias /data/w3/images/$1;
    }
    

    注意点: 当我们使用了正则,然后使用 alias ,我们必须在 alias 中使用正则的变量,

    实际测试

    nginx 版本: 1.12.2

    当 nginx location 正则 aliastry_files, 存在排斥情况。

    示例

    访问URL: /mainland/pgc/index.html

    	location ~ (/(zh-cn|zh-hk|en|mainland))?/pgc/(.*) {
                 alias /www/dev/mainland/pgc/$3;
                 try_files $uri /mainland/pgc/$3;
                 index index.html index.htm;
        }
    

    上面正则中 $1 和$2 都是 mainland, $3的值为 index.html

    根据上面配置后, 访问 /mainland/pgc/index.html ,报错为

    2021/08/30 16:38:32 [error] 18570#0: *1249587 rewrite or internal redirection cycle while internally redirecting to "/mainland/pgc/index.html", client: 10.210.20.112, server: usmart-dev.baidu.com, request: "GET /mainland/pgc/index.html HTTP/1.1", host: "usmart-dev.baidu.com"
    

    当我去除掉 try_files, 也就是如下配置时

    	location ~ (/(zh-cn|zh-hk|en|mainland))?/pgc/(.*) {
                 alias /www/dev/mainland/pgc/$3;
                 index index.html index.htm;
        }
    

    访问 /mainland/pgc/index.html ,正常。

    正则使用变量名

    	location ~ (?<product>/(zh-cn|zh-hk|en|mainland))?/pgc/(?<page>.*) {
                 alias /www/dev/mainland/pgc/$page;
                 index index.html index.htm;
        }
    

    try_files

    http://nginx.org/en/docs/http/ngx_http_core_module.html#try_files

    语法

    Syntax:	try_files file ... uri;
    try_files file ... =code;
    Default:	—
    Context:	server, location
    

    按指定顺序检查文件是否存在,并使用第一个找到的文件进行请求处理。处理的基于上下文处理的。文件的路径是file 根据前面的 root 和 alias 指令作为参数构建的。可以通过在名称末尾,指定斜杠来检查目录是否存在, 例如 $uri/, 如果没有找到任何文件, URL 则进行到最后一个参数中指定的内部重定向。 最后一个参数可以是URL 也可以是一个 code.

    URL:

    location / {
        try_files $uri $uri/index.html /test/index.html;
    }
    

    code:

    location / {
        try_files $uri $uri/index.html $uri.html =404;
    }
    

    Example in proxying Mongrel:

    location / {
        try_files /system/maintenance.html
                  $uri $uri/index.html $uri.html
                  @mongrel;
    }
    
    location @mongrel {
        proxy_pass http://mongrel;
    }
    

    一般配置:

    try_files $uri $uri/ /index.html;
    
    作者:理想三旬
    出处:
    如果觉得文章写得不错,或者帮助到您了,请点个赞,加个关注哦。运维学习交流群:544692191
    本文版权归作者所有,欢迎转载,如果文章有写的不足的地方,或者是写得错误的地方,请你一定要指出,因为这样不光是对我写文章的一种促进,也是一份对后面看此文章的人的责任。谢谢。
  • 相关阅读:
    真正的e时代
    在线手册
    UVA 10616 Divisible Group Sums
    UVA 10721 Bar Codes
    UVA 10205 Stack 'em Up
    UVA 10247 Complete Tree Labeling
    UVA 10081 Tight Words
    UVA 11125 Arrange Some Marbles
    UVA 10128 Queue
    UVA 10912 Simple Minded Hashing
  • 原文地址:https://www.cnblogs.com/operationhome/p/15212691.html
Copyright © 2011-2022 走看看