EDUCBA

EDUCBA

MENUMENU
  • Blog
  • Free Courses
  • All Courses
  • All in One Bundle
  • Login
Home Software Development Software Development Tutorials Software Development Basics Nginx Include

Nginx Include

Introduction to Nginx Include

Nginx include makes our configuration easier to maintain; include is used to split our configuration files into a set featuring specific files stored in the /etc/nginx/ nginx.conf configuration files directory. Using the nginx include directive, we can reference the content for the feature-specific files. Nginx includes a directive to include files or match the specified mask into our configuration files.

Nginx Include

What is Nginx Include?

While using nginx include directive, the included file should consist of directives and blocks which was syntactically correct. We can use the included files anywhere in our configuration files to help to be more readable and enable us to reuse parts of our configuration. When using nginx includes, we need to ensure that our nginx configuration is syntactically correct for the configuration directives and blocks. By using nginx include, we are specifying the path of the included files, which we are including by using the include directive. It is essential and valuable to include files.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Why Use Nginx Include?

We will include a directive for adding the extra configuration files per server blocks we use in our project. For example, suppose we have placed the server block settings specific configuration files in a particular location that is readable and writable for the server block; if the configuration file’s location is correct, it will make the automatic changes.

The nginx configuration file default will include the /etc/nginx/conf.d/ if suppose we have placed our configuration files in that location, then we do not need to involve directives in our configuration files. However, suppose we do not include our configuration file in a specified location. In that case, we need to add the include directive into the configuration files to add or load the configuration files.

Suppose our default configuration file is too big; it isn’t easy to find the words and manage the files. The below example shows how we can include the file by using the include directive in nginx.

Code:

include conf.d/http;
include conf.d/stream;
include conf.d/exchange-enhanced;

Output:

how we can include the file by using the include directive in nginx

We can use the different types of wildcard operators by using the include directive in nginx. In the below example, we can see that if suppose we want to specify only a single file, then we are giving the whole path of the file; also, we are providing the correct file name to find the file accurately if suppose we have not given the correct filename then it will not load the configuration files correctly it will show the syntax error to us.

Code:

include /etc/nginx/conf.d./include.types;
include /etc/nginx/conf.d/stream
include /etc/nginx/conf.d/http;

Output:

Nginx Include 2

In the above example, we can see that nginx will search only that specified file into that location after giving the specified path and name into the configuration file. Hence, the syntax errors chances are very low, and we can easily find our file.

Suppose we don’t know the name of our specified configuration file, which we are adding to the configuration file by using the include directive. We can use the * before the configuration files. We can also use the asterisk (*) sign before the configuration filename, which we include in the configuration files. While using the asterisk sign before the filename, it will search from all the files containing the specified file, and if suppose they have found multiple files with the same name, then all the files will be added by using the include directive in nginx. The example below shows a user of an include directive using an asterisk sign in nginx.

Code:

include /etc/nginx/conf.d/*.conf;

Output:

user of an include directive using an asterisk sign in nginx.

Nginx Include Configuration

Below steps shows how we include the nginx configuration as follows:

In the example below, we are defining how to include the configuration files using the include directive. To use configuration, we need to install nginx in our system; we can install the nginx server using source, rpm, and source installation.

1. In the first step of nginx, including configuration, we install the nginx server. We can install the server of nginx by using the apt-get command in a ubuntu system. Suppose we use another Linux flavor; then, we can also use the rpm or yum command to install the nginx server.

Code:

apt-get install nginx

Output:

install the nginx server

2. The nginx server is not started by default; after installing the same on the ubuntu system, we need to start it manually; we can start the nginx server by using the “service nginx start” command. After beginning the nginx server, we can check the status of the nginx server by using the “service nginx status” command. In the below example, we can see the version of the nginx server, and also, we can see the module which we included in the nginx server.

Code:

service nginx start
service nginx status
nginx –V

Output:

version of the nginx server, and also, we can see the module

3. After starting and checking the status of the nginx server, we create the directory and the configuration file for including the same into the config file.

Code:

mkdir -p /var/www/include/html
vi /etc/nginx/sites-available/nginx_include.conf

Output:

directory and the configuration file

4. After creating the directory and configuration files now, we open the configuration files for adding the configuration files by using the include directive.

adding the configuration files by using the include directive

5. After opening the file now, we include this file into the configuration file by using the include directive. In the example below, we are using the file’s full name; we are not using any asterisk sign to include the file.

Code:

include /etc/nginx/sites-available/nginx_include.conf

Output:

full name of file

6. In the below example, we are using the asterisk sign to add our file into the configuration files as follows.

Code:

include /etc/nginx/sites-available/*.conf

Output:

asterisk sign to add our file into the configuration files

7. After including the file using the include directive now, we are checking the config file’s syntax and restarting the nginx server.

Code:

nginx –t
service nginx restart

Output:

checking the config file's syntax and restarting the nginx server

Conclusion

We will include a directive for adding the extra configuration files per server blocks we use in our project. Nginx includes directives to include files or match the specified mask into our configuration files.

Recommended Articles

This is a guide to Nginx Include. Here we discuss the introduction; why use nginx include? And configuration, respectively. You may also have a look at the following articles to learn more –

  1. Nginx Add_header
  2. Nginx Location Directive
  3. Nginx WAF
  4. Nginx FastCGI
PROGRAMMING LANGUAGES Course
502+ Hours of HD Videos
54 Courses
4 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
SELENIUM Certification Course
57+ Hours of HD Videos
15 Courses
9 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
IOT System - Design & Develop an IOT System
65+ Hours of HD Videos
7 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
JENKINS Certification Course
19+ Hours of HD Videos
6 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
Primary Sidebar
Popular Course in this category
NGINX Course
 8+ Hours of HD Videos
4 Courses
Verifiable Certificate of Completion
  Lifetime Access
4.5
Price

View Course
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

© 2023 - 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
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
Let’s Get Started

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
EDUCBA

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

Forgot Password?

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

🚀 Hurry! - Any Learning Path @ $19 | OFFER ENDING IN ENROLL NOW