Updated February 14, 2023
Introduction to NGINX if
Nginx if is used to define multiple conditions. The directive has a problem when used in the location context; in some cases, it will not do what we expect. It is essential to define that nginx if it is not consistent; identical requests do not randomly fail on other requests with understanding and proper testing. Therefore, Nginx is essential and helpful in defining conditions.
What is NGINX if?
It is evaluated at the time of giving the true condition; this directive of the module is specified inside the braces are executed, and then the request will assign configuration into the direction of the if condition. Configuration was inside the directive of will inherited from the previous configuration level. We can use any condition with it, like when giving false as the variable name, the variable’s value is empty or zero. We can also use the variable comparison string as = and != operators.
When to Use NGINX if?
Variable matching against the regular expression by using the ~ and ~* operators. Regular expressions contain and which were available for later use and variables. Negative operators of ~ and *~ are available in a regular expression, including the} characters; the expression will be enclosed in double or single quotes. As we know that nginx is the web server used with PHP, we can run the nginx and php on the same server. By using nginx and PHP, we can host multiple websites on a single server. For example, we can nginx if while defining conditions.
How to Use NGINX if?
The most common conditional statement in nginx is if, and we can say that we can catch on to it. Using the return status code of 405 is not allowed in the post request. The below example shows the return of the status code with the post request.
Code:
location / {
proxy_cache nginx_if;
proxy_pass http:// bluegrid.io;
proxy_cache_valid 100 nginx_id;
if ($request_method = POST ) {
return 405;
}
}
Output:
The below example shows how nginx is working with the post request. While executing the curl command on the bluegrid.io URL, it will show the below output. In the below example, we can see that the curl command is showing the nginx server version as nginx/1.18.0, which was installed on the ubuntu system; also, it will show the content type as text/html, content length as 178, connection as keeping alive and location is showing as bluegrid.io url.
Code:
# curl -I http://bluegrid.io/ -X POST
HTTP/1.1 301 Moved Permanently
Server : nginx/1.18.0 (Ubuntu)
Date : Thu, 23 Jun 2022 06:23:37 GMT
Content-Type : text/html
Content-Length : 178
Connection : keep-alive
Location : https://bluegrid.io/
Output:
The example below shows how to redirect the request using the query string as p = 66. In the below example, we have used proxy_cache name as nginx_if; also, we have defined the proxy_pass url as bluegrid.io and used proxy_cache_valid as 100. In the if statement, we have defined the argument as p = 66.
Code:
location / {
proxy_cache nginx_if;
proxy_pass http:// bluegrid.io;
proxy_cache_valid 100 nginx_id;
if ($args ~ p = 66 ) {
rewrite ^ http://bluegrid.io/ ;
}
}
Output:
The below example shows how to redirect the request by using the query string as p = 66. While executing the curl command on the bluegrid.io URL, it will show the below output. In the below example, we can see that the curl command is showing the nginx server version as nginx/1.18.0, which was installed on the ubuntu system; also, it will show the content type as text/html, content length as 178, connection as keeping alive and location is showing as bluegrid.io url.
Code:
# curl -I http://bluegrid.io/?p=66
HTTP/1.1 301 Moved Permanently
Server: nginx/1.18.0 (Ubuntu)
Date: Thu, 23 Jun 2022 06:44:41 GMT
Content-Type: text/html
Content-Length: 178
Connection: keep-alive
Location: https://bluegrid.io/?p=66
Output:
In the below example, we are trying to get the header of the response test by passing the value empty. So the header of the request is testing. In the below example, we are using proxy_cache name as nginx_if; also, we have defined the proxy_pass url as bluegrid.io and used proxy_cache_valid as 100; in the if statement, we have defined add_header as empty.
Code:
location / {
proxy_cache nginx_if;
proxy_pass http:// bluegrid.io;
proxy_cache_valid 100 1d;
if ($http_testing = ‘1’) {
add_header ;
}
}
Output:
The below example shows the get header of the response test by passing the value empty. While executing the curl command on the bluegrid.io URL, it will show the below output. The below example shows that the curl command shows the nginx server version as nginx/1.18.0, which was installed on the ubuntu system.
Code:
# curl -I http://bluegrid.io/ -H 'Testing: 1'
HTTP/1.1 301 Moved Permanently
Server: nginx/1.18.0 (Ubuntu)
Date: Thu, 23 Jun 2022 07:01:28 GMT
Content-Type: text/html
Content-Length: 178
Connection: keep-alive
Location: https://bluegrid.io/
Output:
Statement
While using the if statement, the condition is evaluated as true, the directive module is specified inside the braces, and the request is assigned to the configuration of the if directive. The if directive configuration is inherited from the level of the previous configuration. We can use the try files if they suit our needs. We can also use the last and rewrite in other cases and move the same on a server level.
Code:
location /tmp {
set $true 1;
if ($true) {
add_header X-First 1;
}
if ($true) {
add_header X-Second 2;
}
return 204;
}
location /tmp1 {
tmp_files /file @fallback;
set $true 1;
if ($true) {
}
}
location /tmp2 {
set $true 1;
if ($true) {
cgi 127.0.0.1:9000;
}
if ($true) {
}
}
Output:
Conclusion
Configuration was inside the directive of will inherited from the previous configuration level. It is used to define multiple conditions. Unfortunately, the directive has a problem when used in the location context; sometimes, it will not do what we expect.
Recommended Articles
This is a guide to NGINX if. Here we discuss the introduction and how to use it with the statement for better understanding. You may also have a look at the following articles to learn more –