Nginx Configuration Guide: From Scratch
Nginx, short for "engine x," is a popular open-source web server known for its high performance, stability, and low resource consumption. It's widely used as a reverse proxy, load balancer, and web server itself, serving millions of websites worldwide.
This guide will walk you through configuring Nginx from scratch, covering the basics and offering practical examples and best practices to get you started.
Understanding Nginx Architecture
Nginx operates as a daemon, running in the background and listening for incoming requests. When a request arrives, Nginx parses it, determines the appropriate server block configuration, and either serves the static content directly or forwards the request to an upstream server.
Key Components:
- Master Process: Manages worker processes and handles configuration loading.
- Worker Processes: Handle client requests concurrently, improving performance and scalability.
- Event Handling: Nginx utilizes an event-driven architecture, efficiently managing connections and handling requests asynchronously.
Getting Started
1. Installation:
Nginx installation varies depending on your operating system. Here's a general guide for popular platforms:
-
Ubuntu/Debian:
sudo apt update sudo apt install nginx
-
CentOS/RHEL:
sudo yum update sudo yum install nginx
2. Accessing the Configuration File:
The primary Nginx configuration file is located at /etc/nginx/nginx.conf
.
Basic Nginx Configuration
The nginx.conf
file is structured using directives, which control various aspects of Nginx's behavior.
Syntax:
directive_name value;
Example:
user www-data;
worker_processes auto;
# Define a server block
server {
listen 80;
server_name example.com;
# Define a location block for the root directory
location / {
root /var/www/example.com;
index index.html;
}
}
Explanation:
-
user www-data;
: Specifies the user Nginx runs as. -
worker_processes auto;
: Automatically determines the number of worker processes based on system resources. -
server { ... }
: Defines a virtual server. -
listen 80;
: Specifies the port Nginx listens on (default HTTP port). -
server_name example.com;
: Sets the domain name for this server block. -
location / { ... }
: Defines a location block, handling requests for the root path (/
). -
root /var/www/example.com;
: Sets the document root for this location. -
index index.html;
: Specifies the default HTML file to serve.
3. Testing:
After making changes to the configuration, reload Nginx:
sudo systemctl reload nginx
Access your website in a browser (e.g., http://example.com
) to verify the changes.
Advanced Configuration
1. Virtual Hosts:
Create multiple server blocks within the nginx.conf
file to host different websites or services on the same server.
server {
listen 80;
server_name website1.com;
root /var/www/website1;
index index.html;
}
server {
listen 80;
server_name blog.website1.com;
root /var/www/blog;
index index.php;
}
2. SSL/TLS Encryption:
Secure your website using SSL/TLS certificates. Configure a server block with:
ssl on;
: Enables SSL/TLS.ssl_certificate /path/to/certificate.crt;
: Specifies the certificate file.ssl_certificate_key /path/to/private_key;
: Specifies the private key file.
3. Reverse Proxy:
Forward requests to upstream servers (e.g., application servers) using the proxy_pass
directive.
location /api {
proxy_pass http://backend_server:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
4. Load Balancing:
Distribute traffic efficiently across multiple upstream servers using load balancing techniques like round robin or least connections.
5. Caching:
Improve performance by caching static content. Configure caching directives like proxy_cache
and proxy_cache_path
.
Best Practices
- Security: Keep Nginx updated, use SSL/TLS, and restrict access to sensitive configuration files.
- Performance: Configure worker processes appropriately, utilize caching, and optimize server blocks.
- Logging: Enable detailed logging for troubleshooting and monitoring.
- Testing: Thoroughly test your configurations before deployment.
- Documentation: Document your configuration changes for future reference.
Conclusion
Nginx offers a powerful and flexible platform for web serving and related tasks. This guide has provided a foundational understanding of its configuration, enabling you to get started with building robust and scalable web applications.