Catalog / Nginx Cheatsheet
Nginx Cheatsheet
A quick reference guide for Nginx, covering configuration, common directives, and useful commands for managing your web server.
Basic Configuration
Configuration File Structure
Nginx configuration files are typically located in
|
The main configuration file (
|
Essential Directives
|
Specifies the port (and optionally address) that the server should listen on. |
|
Defines the domain names that this server block should handle. |
|
Specifies the root directory for serving files. |
|
Defines the default files to serve if no specific file is requested. |
|
Configures how Nginx handles requests for specific URIs. |
|
Specifies the file to log errors to and the log level. |
|
Specifies the file to log access requests to. |
Example Server Block
|
Common Commands
Service Management
Start Nginx |
|
Stop Nginx |
|
Restart Nginx |
|
Reload Nginx |
|
Check Nginx status |
|
Enable Nginx on boot |
|
Disable Nginx on boot |
|
Configuration Testing
Test Nginx configuration file for syntax errors before applying changes:
|
If the configuration is valid, you’ll see output like:
|
Reverse Proxy & Load Balancing
Basic Reverse Proxy
Nginx can act as a reverse proxy, forwarding client requests to backend servers.
|
Explanation of directives:
|
Load Balancing
Nginx can distribute traffic across multiple backend servers using load balancing.
|
Different load balancing methods can be specified using the
|
SSL/TLS Configuration
Basic SSL Configuration
To enable SSL/TLS, you need an SSL certificate and key. You can obtain these from a Certificate Authority (CA) or generate a self-signed certificate (for testing purposes only).
|
Configure the server block to listen on port 443 and specify the certificate and key files.
|
Redirect HTTP to HTTPS
To automatically redirect HTTP traffic to HTTPS, add a separate server block for port 80.
|
Optimizing SSL Configuration
Use strong ciphers and enable HTTP Strict Transport Security (HSTS) to improve security.
|