Nginx

NGINX If Else

NGINX directives play a critical role in the overall performance and functionality of the NGINX server. They can control various server functions, from setting up the virtual servers and load balancing to implementing the HTTP security headers, SSL/TLS encryption, and managing the access to resources based on client IP or authentication status.

In this tutorial, we’ll explore how to use the “if” directive in NGINX. First, it’s important to note that the “if” directive works differently in NGINX than in most programming languages, and the misuse of it can lead to unexpected results.

NOTE: Before using the “if” directive in NGINX, we recommend that you read this following post which is published in the official NGINX docs.

Source: NGINX Documentation

Installing NGINX

Before discussing the syntax and usage of the “if” directive in NGINX, install it on your server. For this tutorial, we use an Ubuntu 22.04 server.

Run the following commands as follows:

sudo apt update
sudo apt install nginx

NGINX If Directive

The following shows the syntax of the “if” directive in NGINX:

location /some/path {
    if ($variable = value) {
        # Actions
    }
}

In this case, NGINX performs the defined actions within the “if” block if the $variable is equal to the value.

Use the NGINX “If” Directive to Redirect to a Different URL

One example is using the “if” block to redirect the users with a given IP address to a different URL. We can use the “if” directive as follows:

server {
    listen 80;
    server_name example.com;

    location / {
        if ($remote_addr = " 84.88.66.29 ") {
            rewrite ^ http://another-url.com redirect;
        }
    }

}

In the previous example, if a user with the 84.88.66.29 IP address tries to access the “example.com”, the server redirects that request to http://another-url.com with a 302 status code.

Use the NGINX If Directive to Return a Custom Error Page

We can also use the “if” directive to show a custom error page in the server. An example configuration is as follows:

server {
    listen 80;
    server_name example.com;
    error_page 404 /404.html;

    location / {
        if ($request_filename !~ "-f") {
            return 404;
        }
    }

    location = /404.html {
        root /var/www/html;
        internal;
    }

}
}

In this case, if the requested resource does not exist on the server, NGINX redirects the user to the “404.html” file which is defined in the server root.

Once you make changes to the NGINX configuration file, it is good to verify it for syntax errors with the following command:

$ sudo nginx -t

To apply the changes, use the reload command:

$ sudo systemctl reload nginx

Conclusion

This tutorial covered the basics of working with the “if” directive in NGINX to introduce the conditional operations. Unfortunately, the “if” directive is executed at runtime, not at request time, which may lead to unexpected behavior.

“The “if” directive has problems when used in the location context. Sometimes, it doesn’t do what you expect but it does something entirely different. In some cases, it even segfaults. So, it’s generally a good idea to avoid it if possible.” – Quoted from the Official NGINX docs.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list