EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

NGINX if

Secondary Sidebar
Nginx Tutorial
  • Basic
    • Nginx Add_header
    • Nginx FastCGI
    • Nginx WAF
    • Nginx Forward Proxy
    • Nginx Ingress Annotations
    • Nginx Location Directive
    • Nginx User
    • Nginx Port
    • Nginx Auth_request
    • Nginx Autoindex
    • Nginx if else
    • Nginx uWSGI
    • Nginx X-Forwarded-For
    • Nginx GUI
    • NGINX Unit
    • NGINX if
    • Nginx default_server
    • Nginx worker_connections
    • Nginx Authentication
    • Nginx Gateway Timeout
    • Nginx Error_page
    • Nginx Server Block
    • Nginx Forbidden
    • Nginx Modules
    • Nginx Max Upload Size
    • Nginx vs LiteSpeed
    • Nginx multiple server_name
Home Software Development Software Development Tutorials Nginx Tutorial NGINX if

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.

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,064 ratings)

NGINX if

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.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

When Using 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
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

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

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

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

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

Special Offer - All in One Software Development Bundle (600+ Courses, 50+ projects) Learn More