Question

conditional within an "if" in Nginx

Is it possible to do more than one comparison within an “if” in Nginx? For instance in a language like JAVA we use “if(comparison 1 || comparison 2)” and the || works as an “or”

Show comments

Submit an answer

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

You can also use something like:

    if ($request_uri = /) {
      set $test "root";
    }

    if ($host ~* zencocoon.com) {
      set $test  "${test}+zencocoon.com";
    }

    if ($http_cookie !~* "auth_token") {
      set $test  "${test}+no_auth_token";
    }

    if ($test = "root+zencocoon.com+no_auth_token") {
      proxy_pass http://www.zencocoon.com;
      break;
    }

Taken from https://gist.github.com/jrom/1760790

In general, if statements should be avoided when possible, particularly inside location blocks. From the aptly titled “If Is Evil”:

Directive if has problems when used in location context, in some cases it doesn’t do what you expect but something completely different instead. In some cases it even segfaults. It’s generally a good idea to avoid it if possible.

The only 100% safe things which may be done inside if in location context are:

  • return ...;
  • rewrite ... last;

Anything else may possibly cause unpredictable behaviour, including potential SIGSEGV.

As to your specific question, you can leverage regex to have multiple matches in the condition but not multiple conditions. So you can do something like:

if ($geoip_country_code ~ (BR|CN|KR|RU|UA) ) {
    # Do something
}

but not:

if ($geoip_country_code ~ (US|CA) ) | ($http_cookie ~* "CCCC=.+(?:;|$)"  ) {
    # Do something else
}

There are some workarounds using multiple if statements. Check out this post.

Using so many ‘if’ is bad practice and in most case means misunderstanding of nginx way configuration, the real power of nginx.

  1. Regarding if ($request_uri = /) {}

Writing such statement means you do not understand nginx ‘location’ directive. Location is already optimized for checking $request_uri againt expressions. It works faster and safer.

So your statement can be replaced with more proper location = / { }

  1. Regarding other ‘if’ - in most cases it can be reduced with nginx ‘map’. This statement:

if ($host ~* zencocoon.com) { set $test “${test}+zencocoon.com”; }

if ($http_cookie !~* "auth_token") {
  set $test  "${test}+no_auth_token";
}

if ($test = "root+zencocoon.com+no_auth_token") {
  proxy_pass http://www.zencocoon.com;
  break;
}

Can be rewritten with

map “$hostname:$http_cookie” $pass_to_proxy { default 0; “~*zencocoon.com:?!auth_token” 1; }

if ($pass_to_proxy) { proxy_pass http://www.zencocoon.com; break; }

It’s more short, safe, elegant and nginx-way.