如果条件在nginx配置中破坏了try_files
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了如果条件在nginx配置中破坏了try_files,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3008字,纯文字阅读大概需要5分钟。
内容图文
![如果条件在nginx配置中破坏了try_files](/upload/InfoBanner/zyjiaocheng/971/4d09ed2637f7496c9e724edd01ffc69d.jpg)
我的nginx配置中有一个简单的位置块,它匹配我网站的静态文件.我想要做的是使用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指令为什么不好并且应该避免的解读,但我仍然不知道为什么它会破坏我的配置.如果我理解正确,它与if或add_header是重写模块的一部分或类似的东西有关,我猜这与try_files冲突.也许我在这里不准确,但无论哪种方式我都不确定如何解决它.
为什么if和/或add_header的存在使得nginx在找不到文件时给我404而不是301,我该如何修复它?提前致谢!
解决方法:
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,那么您之前添加的任何标题都会被遗忘,它会在新的位置块中再次启动,因此需要在那里添加标题.
至于如何解决它;你可以在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;
}
内容总结
以上是互联网集市为您收集整理的如果条件在nginx配置中破坏了try_files全部内容,希望文章能够帮你解决如果条件在nginx配置中破坏了try_files所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。