代码之家  ›  专栏  ›  技术社区  ›  Marged

位置不适用于文件,但仅适用于路径

  •  1
  • Marged  · 技术社区  · 6 年前

    server {
      ...
      root /var/opt/data/web;
      ...
    
      location ~* \.(?:eot|woff|woff2|ttf|js)$ {
        expires 1M;
      }
    
      ...
    
      location /one {
        root /var/opt/data/alternatives;
        try_files $uri $uri/ =404;
      }
    
      location /two {
        root /var/opt/data/alternatives;
        try_files $uri $uri/ =404;
      }
    }
    

    当我 curl http://localhost/one/ 我得到了 index.html /other . 但当我卷曲 .../localhost/one/foo.js

    open()“/default/foo.js”失败(2:没有这样的文件或目录)

    location ~ (one|two) , location /one/ 甚至 location ~ /(one|two) 但都没用。

    location s、 但我想我的问题是因为我设置的位置 .js 资源 expire -1 因为这样可以防止根变成我需要的。

    如果这很重要:我使用nginx1.15.2。万一你想知道为什么我会有这种奇怪的 alternatives 目录:the web 选择 git pull 预计起飞时间。

    1 回复  |  直到 6 年前
        1
  •  1
  •   Richard Smith    6 年前

    nginx 选择一个 location process a request . 你的 location ~* \.(?:eot|woff|woff2|ttf|js)$ 块处理以 .js ,及其 root 值从外部块继承为 /var/opt/data/web .

    如果你有多个根,你需要确保 位置 ^~ 修饰语。看到了吗 this document 详情。

    例如:

    server {
        ...
        root /var/opt/data/web;
        ...    
        location ~* \.(?:eot|woff|woff2|ttf|js)$ {
            expires 1M;
        }    
        ...
        location ^~ /one {
            root /var/opt/data/alternatives;
            try_files $uri $uri/ =404;
    
            location ~* \.(?:eot|woff|woff2|ttf|js)$ {
                expires 1M;
            }    
        }
        ...
    }
    

    如果你需要 expires 位置


    另一种选择是 到期 map this document 详情。

    map $request_uri $expires {
        default                            off;
        ~*\.(eot|woff|woff2|ttf|js)(\?|$)  1M;
    }
    server {
        ...
        root /var/opt/data/web;
        expires $expires;
        ...
        location ^~ /one {
            root /var/opt/data/alternatives;
            try_files $uri $uri/ =404;
        }
        ...
    }
    
    推荐文章