EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Software Development Software Development Tutorials Nginx Tutorial NGINX if
 

NGINX if

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.

 

 

NGINX if

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

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:

return of the status code with the post request

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:

executing the curl command on the bluegrid.io URL

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:

how to redirect the request using the query string as p = 66

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:

executing the curl command on the bluegrid.io URL

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:

we are trying to get header of response test

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:

NGINX if 7

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:

directive module is specified inside the braces

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 –

  1. Add_header
  2. Nginx Location Directive
  3. Nginx WAF
  4. Nginx FastCGI

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

🚀 Limited Time Offer! - ENROLL NOW