Insanity: IF clause breaks LOCATION clause

2019-08-28 02:54发布

问题:

With nginx.conf including the following

     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

They both retrieve http://otherhost/admin.html

When I change nginx.conf to read:

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

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.

Huh?

回答1:

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)



标签: nginx