NGINX Cheat Sheet: A Quick Reference Guide
NGINX is a powerful and versatile web server that is widely used to serve static content, reverse proxy, and load balance across servers. Whether you’re a beginner or an experienced user, having a cheat sheet handy can save time and help you navigate it’s configurations efficiently. Let’s dive into a quick reference guide for it.
Installation and Basic Commands of NGINX
Nginx Installation
sudo apt-get update
sudo apt-get install nginx
Start/Stop/Restart
sudo service nginx start
sudo service nginx stop
sudo service nginx restart
Check NGINX Configuration
nginx -t
Configuration File Locations of Nginx
- Main configuration file:
/etc/nginx/nginx.conf
- Server block configuration:
/etc/nginx/sites-available/
- Enabled server block symlink:
/etc/nginx/sites-enabled/
Server Blocks of Nginx
Basic Server Block Structure
server {
listen 80;
server_name example.com www.example.com;
location / {
# Configuration for handling requests
}
}
Redirect HTTP to HTTPS
server {
listen 80;
server_name example.com www.example.com;
return 301 https://$host$request_uri;
}
NGINX Locations and Directives
Root Directive
location / {
root /path/to/your/files;
index index.html;
}
Proxy Pass
location /app {
proxy_pass http://backend_server;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
URL Rewriting
location /blog {
rewrite ^/blog/(.*)$ /$1 break;
}
NGINX SSL Configuration
SSL Certificate
ssl_certificate /path/to/your/certificate.crt;
ssl_certificate_key /path/to/your/private.key;
Enable SSL
server {
listen 443 ssl;
server_name example.com;
# SSL configuration here
}
SSL Redirect
server {
listen 80;
server_name example.com;
return 301 https://$host$request_uri;
}
Load Balancing
Round Robin Load Balancing
upstream backend {
server backend1.example.com;
server backend2.example.com;
}
server {
location / {
proxy_pass http://backend;
}
}
This cheat sheet covers essential Nginx configurations, but remember to consult the official documentation for more in-depth details. Nginx’s flexibility allows it to be used in various scenarios, making it a crucial tool for web server management.