Nginx Cheatsheet
A quick reference for common Nginx commands and configuration directives.
Basic Commands
Command / Directive | Description |
---|---|
sudo systemctl start nginx |
Start the Nginx service. |
sudo systemctl stop nginx |
Stop the Nginx service. |
sudo systemctl restart nginx |
Restart the Nginx service. |
sudo systemctl reload nginx |
Reload the configuration without dropping connections. |
sudo nginx -t |
Test the Nginx configuration for syntax errors. |
Location Blocks
Command / Directive | Description |
---|---|
location / { ... } |
Matches any request. |
location = /exact { ... } |
Matches only the exact path /exact. |
location ~ \.php$ { ... } |
Matches any request ending in .php (case-sensitive regex). |
location ~* \.(gif|jpg|png)$ { ... } |
Matches requests for common image formats (case-insensitive regex). |
Proxying
Command / Directive | Description |
---|---|
proxy_pass http://backend; |
Pass requests to a backend server. |
proxy_set_header Host $host; |
Pass the original host header to the backend. |
proxy_set_header X-Real-IP $remote_addr; |
Pass the client's real IP to the backend. |
SSL/TLS (HTTPS)
Command / Directive | Description |
---|---|
listen 443 ssl; |
Listen for SSL connections on port 443. |
ssl_certificate /path/to/fullchain.pem; |
Path to your SSL certificate. |
ssl_certificate_key /path/to/privkey.pem; |
Path to your private key. |
Server Block (Virtual Host)
Command / Directive | Description |
---|---|
server { ... } |
Defines a virtual server to handle requests. |
listen 80; |
Listen for connections on port 80. |
server_name example.com www.example.com; |
Define the server names for this block. |
root /var/www/example.com; |
Set the root directory for requests. |
index index.html index.htm; |
Define the default file to serve. |