In this section, we will cover the fundamental aspects of configuring NGINX. By the end of this module, you will understand how to set up a basic NGINX configuration file, define server blocks, and manage essential settings.
Key Concepts
- NGINX Configuration File Structure
- Server Blocks
- Location Blocks
- Directives and Contexts
- NGINX Configuration File Structure
NGINX configuration files are typically located in the /etc/nginx/
directory. The main configuration file is nginx.conf
. The structure of an NGINX configuration file is hierarchical and consists of directives and contexts.
Example Configuration File
user nginx; worker_processes 1; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; location / { root /usr/share/nginx/html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } } }
Explanation
- user: Defines the user and group that the NGINX worker processes will run as.
- worker_processes: Specifies the number of worker processes.
- events: Contains directives that affect the NGINX event model.
- http: This context includes directives for handling HTTP traffic.
- Server Blocks
Server blocks are used to define different virtual servers. Each server block can handle requests for a specific domain or IP address.
Example Server Block
server { listen 80; server_name example.com www.example.com; location / { root /var/www/html; index index.html index.htm; } error_page 404 /404.html; location = /404.html { root /var/www/html; } }
Explanation
- listen: Specifies the port that the server will listen on.
- server_name: Defines the domain names that this server block will respond to.
- location: Defines how to process requests for a specific URI.
- Location Blocks
Location blocks are used to define how NGINX should process requests for specific URIs.
Example Location Block
Explanation
- location: The URI prefix that this block will match.
- root: The directory where the content is located.
- Directives and Contexts
Directives are instructions that tell NGINX how to behave. They can be placed in different contexts, such as http
, server
, and location
.
Common Directives
Directive | Contexts | Description |
---|---|---|
listen |
server |
Specifies the port and address to listen on. |
server_name |
server |
Defines the domain names for the server block. |
root |
http , server , location |
Sets the root directory for requests. |
index |
http , server , location |
Specifies the index file for directories. |
error_page |
http , server , location |
Defines custom error pages. |
Practical Exercise
Task
- Create a new server block in the
nginx.conf
file that listens on port 8080 and serves content from/var/www/test
. - Define a custom 404 error page located at
/var/www/test/404.html
.
Solution
server { listen 8080; server_name test.example.com; location / { root /var/www/test; index index.html index.htm; } error_page 404 /404.html; location = /404.html { root /var/www/test; } }
Explanation
- The
listen
directive is set to8080
to listen on port 8080. - The
server_name
is set totest.example.com
. - The
location /
block serves content from/var/www/test
. - The
error_page
directive defines a custom 404 error page.
Common Mistakes and Tips
- Forgetting Semicolons: Each directive must end with a semicolon.
- Incorrect Contexts: Ensure directives are placed in the correct context.
- File Permissions: Make sure NGINX has the necessary permissions to access the files and directories specified.
Conclusion
In this section, we covered the basics of NGINX configuration, including the structure of the configuration file, server blocks, location blocks, and common directives. Understanding these fundamentals is crucial for effectively managing and configuring NGINX. In the next module, we will explore how to use NGINX as a web server to serve static content and more.