疯狂:IF 子句打破了 LOCATION 子句 [英] Insanity: IF clause breaks LOCATION clause

查看:66
本文介绍了疯狂:IF 子句打破了 LOCATION 子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 nginx.conf 包括以下内容

     location /beta/ {
        proxy_pass http://otherhost/;
      }

那么以下两种检索都有效:

then both of the following retrievals work:

  • curl -H 'User-Agent: Mozilla' http://localhost/beta/admin.html
  • curl -H 'User-Agent: iPhone' http://localhost/beta/admin.html

他们都检索http://otherhost/admin.html

当我将 nginx.conf 更改为读取时:

When I change nginx.conf to read:

     location /beta/ {
        if ($http_user_agent ~ iPhone ) {
        }
        proxy_pass http://otherhost/;
      }

然后 curl -H 'User-Agent: Mozilla' http://localhost/beta/admin.html 继续工作,但curl -H 'User-Agent: iPhone' http://localhost/beta/admin.html 给出 404 并且其他主机抱怨它没有名为 beta/admin 的文件.html.无论 if 是否为空,都会发生这种情况.

Then curl -H 'User-Agent: Mozilla' http://localhost/beta/admin.html continues to work but curl -H 'User-Agent: iPhone' http://localhost/beta/admin.html gives a 404 and otherhost complains that it doesn't have a file called beta/admin.html. This happens whether if is empty or not.

嗯?

推荐答案

您遇到了一个已知问题:If is evil in nginx,意思是避免 if-blocks inside location-blocks 如果可能的话(链接解释了为什么和怎么)

You're getting bit by a known problem: If is evil in nginx, meaning avoid if-blocks inside location-blocks if at all posible (the link explains why and how)

这篇关于疯狂:IF 子句打破了 LOCATION 子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆