Nginx Cheatsheet
This cheatsheet covers common Nginx commands, configuration structure, directives, and examples, consolidated into a single document.
Core Commands (Systemd Systems - Ubuntu/Debian/CentOS 7+)
Use sudo
before these commands if not running as root.
Command | Description |
---|---|
systemctl start nginx |
Start the Nginx service. |
systemctl stop nginx |
Stop the Nginx service. |
systemctl restart nginx |
Stop and then start the Nginx service. |
systemctl reload nginx |
Reload configuration without stopping the server (graceful reload). |
systemctl status nginx |
Check the current status of the Nginx service. |
systemctl enable nginx |
Enable Nginx to start automatically on system boot. |
systemctl disable nginx |
Disable Nginx from starting automatically on system boot. |
nginx -t |
Test the Nginx configuration files for syntax errors. |
nginx -t -c <config_file> |
Test a specific configuration file. |
nginx -v |
Show Nginx version. |
nginx -V |
Show Nginx version, build parameters, and module information. |
Alternative nginx
commands (less common for service management)
Command | Description |
---|---|
nginx -s stop |
Fast shutdown. |
nginx -s quit |
Graceful shutdown. |
nginx -s reload |
Reload configuration file. |
nginx -s reopen |
Reopen log files. |
Default File & Directory Locations
Locations can vary depending on installation method (package manager, source) and OS distribution. These are common defaults:
- Main Configuration File:
/etc/nginx/nginx.conf
(Most Systems)/usr/local/nginx/conf/nginx.conf
(Compiled from source default)
- Server Block / Virtual Host Configs:
/etc/nginx/sites-available/
(Config files are defined here - Debian/Ubuntu)/etc/nginx/sites-enabled/
(Symlinks to configs insites-available
that should be active - Debian/Ubuntu)/etc/nginx/conf.d/
(Directory for.conf
files, often used by CentOS/RHEL and included bynginx.conf
)
- Default Web Root:
/var/www/html
(Debian/Ubuntu)/usr/share/nginx/html
(CentOS/RHEL)
- Log Files:
/var/log/nginx/access.log
(Access logs)/var/log/nginx/error.log
(Error logs)
- Nginx Binary:
/usr/sbin/nginx
- PID File:
/var/run/nginx.pid
or/run/nginx.pid
Configuration File Structure
- Directives: Configuration options (e.g.,
worker_processes
,listen
). End with a semicolon;
. - Blocks / Contexts: Group directives that apply to a certain scope (e.g.,
http
,server
,location
). Defined by curly braces{}
. - Comments: Lines starting with
#
. - Includes: Load configuration from other files using the
include
directive. Wildcards (*
) can be used.
# Example Structure of nginx.conf (simplified)
user www-data; # Directive: Run worker processes as this user
worker_processes auto; # Directive: Auto-detect number of CPU cores for workers
pid /run/nginx.pid; # Directive: File to store the main process ID
# Events block: Connection processing settings
events {
worker_connections 768; # Max connections per worker process
}
# HTTP block: Defines parameters for HTTP/HTTPS traffic
http {
# Include MIME types definition
include /etc/nginx/mime.types;
# Default MIME type if none matches
default_type application/octet-stream;
# Define a custom log format named 'main'
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
# Set access log path and use the 'main' format
access_log /var/log/nginx/access.log main;
# Set error log path and minimum logging level
error_log /var/log/nginx/error.log warn;
# Performance optimizations
sendfile on; # Use sendfile() for efficient file serving
tcp_nopush on; # Optimize packet sending
tcp_nodelay on; # Disable Nagle's algorithm
# Timeout for keep-alive connections
keepalive_timeout 65;
# Enable gzip compression (requires further gzip_* directives typically)
# gzip on;
# Include server block configuration files
# Common on CentOS/RHEL/Fedora:
include /etc/nginx/conf.d/*.conf;
# Common on Debian/Ubuntu:
include /etc/nginx/sites-enabled/*;
# Example Server Block (Virtual Host) - often in separate included file
# server {
# listen 80;
# server_name example.com www.example.com;
#
# location / {
# root /var/www/html;
# index index.html index.htm;
# }
# }
}