如果条件在nginx配置中破坏了try_files

时间:2016-09-17 15:04:44

标签: nginx

我的nginx配置中有一个简单的location块,它匹配我网站的静态文件。我想要做的是使用try_files检查文件是否存在,如果不存在,则重定向到URL(在这种情况下在@cdn位置块中指定)。我还想设置一些CORS标题。

以下是相关配置。

location ~* \.(css|js|jpe?g|png|gif|otf|eot|svg|ttf|woff|woff2|xml|json)$ {
    if ($request_method = 'OPTIONS') {
        add_header 'Access-Control-Allow-Origin' "$http_origin";
        add_header 'Access-Control-Allow-Methods' 'GET, OPTIONS';
        add_header 'Access-Control-Max-Age' 1728000;
        add_header 'Content-Type' 'text/plain charset=UTF-8';
        add_header 'Content-Length' 0;

        return 204;
    }

    if ($request_method = 'POST') {
        add_header 'Access-Control-Allow-Origin' "$http_origin";
        add_header 'Access-Control-Allow-Methods' 'GET, OPTIONS';
    }

    if ($request_method = 'GET') {
        add_header 'Access-Control-Allow-Origin' "$http_origin";
        add_header 'Access-Control-Allow-Methods' 'GET, OPTIONS';
    }

    try_files $uri @cdn;
}

location @cdn {
    return 301 https://example.com$request_uri;
}

问题是,如果文件不存在,我得到404响应,而不是301重定向。在添加CORS头之前,配置工作正常。如果我删除了标题的处理,一切都按预期工作,我得到301回复。

现在我已经做了一些关于if指令为什么不好而且应该避免的解读,但我仍然不知道为什么它会破坏我的配置。如果我理解正确,它与ifadd_header是重写模块的一部分或类似事件有关,我猜这与try_files冲突。也许我在这里不准确,但无论哪种方式,我都不确定如何解决它。

为什么if和/或add_header的存在使得nginx在找不到文件时给我404而不是301,我该如何解决?提前谢谢!

1 个答案:

答案 0 :(得分:5)

在了解if的工作原理时,您可能会对

http://agentzh.blogspot.co.uk/2011/03/how-nginx-location-if-works.html感兴趣。在您的情况下,当if条件匹配时,请求现在在if上下文中提供,并且try_files不会被该上下文继承。或者https://www.digitalocean.com/community/tutorials/understanding-the-nginx-configuration-file-structure-and-configuration-contexts说“在使用if上下文时要记住的另一件事是它在同一个上下文中呈现try_files指令是无用的。”

此外,如果try_files回退到@cdn,那么您之前添加的所有标题都会被遗忘,它会在新的location块中重新开始,因此标题需要加在那里。

至于如何解决它;你可以在if内设置变量,add_header忽略一个空值,所以这样的东西应该有用:

set $access-control-output 0;
location ~* \.(css|js|jpe?g|png|gif|otf|eot|svg|ttf|woff|woff2|xml|json)$ {
    set $access-control-output 1;
    try_files $uri @cdn;
}

set $acao = "";
set $acam = "";
if ($access-control-output) {
    set $acao = $http_origin;
    set $acam = "GET, OPTIONS";
}

map "$access-control-output:$request_method" $acma {
    "1:OPTIONS" 1728000; 
    default     "";
}

location @cdn {
    add_header 'Access-Control-Allow-Origin' $acao;
    add_header 'Access-Control-Allow-Methods' $acam;
    add_header 'Access-Control-Max-Age' $acma;
    return 301 https://example.com$request_uri;
}

编辑:你不关心@cdn后备中的标题,在这种情况下你应该能够有这样的东西:

map $request_method $acma {
    "OPTIONS" 1728000; 
    default   "";
}

location ~* \.(css|js|jpe?g|png|gif|otf|eot|svg|ttf|woff|woff2|xml|json)$ {
    add_header 'Access-Control-Allow-Origin' $http_origin;
    add_header 'Access-Control-Allow-Methods' "GET, OPTIONS";
    add_header 'Access-Control-Max-Age' $acma;
    try_files $uri @cdn;
}

location @cdn {
    return 301 https://example.com$request_uri;
}
相关问题