Here’s a cheat sheet for working with Nginx, a popular web server and reverse proxy server:
Basic Configuration
- Install Nginx:
- Ubuntu:
sudo apt-get install nginx
- CentOS:
sudo yum install nginx
- Ubuntu:
- Start/Stop/Restart Nginx:
- Start:
sudo service nginx start
- Stop:
sudo service nginx stop
- Restart:
sudo service nginx restart
- Start:
- Nginx Configuration File:
- Default location:
/etc/nginx/nginx.conf
- Default location:
- Test Configuration:
- Check for syntax errors:
nginx -t
- Check for syntax errors:
Server Blocks (Virtual Hosts)
Basic Server Block:
server {
listen 80;
server_name example.com www.example.com;
location / {
# Configurations for the root location
}
}
Multiple Server Blocks:
server {
listen 80;
server_name example.com;
# Configurations for example.com
}
server {
listen 80;
server_name www.example.com;
# Configurations for www.example.com
}
Location Blocks
Basic Location Block:
location / {
# Configurations for the root location
}
Proxy Pass:
location /app {
proxy_pass http://backend_server;
# Additional proxy configurations
}
SSL/TLS Configuration
SSL Certificate Configuration:
server {
listen 443 ssl;
server_name example.com;
ssl_certificate /path/to/certificate.crt;
ssl_certificate_key /path/to/private_key.key;
# Other SSL configurations
}
Here is a dedicated SSL Cheat Sheet.
Reverse Proxy
Basic Reverse Proxy:
location / {
proxy_pass http://backend_server;
# Additional proxy configurations
}
Load Balancing
Load Balancing:
upstream backend {
server backend1.example.com;
server backend2.example.com;
# Additional backend servers
}
server {
location / {
proxy_pass http://backend;
# Load balancing configurations
}
}
Basic Security
Basic Authentication:
location / {
auth_basic "Restricted";
auth_basic_user_file /etc/nginx/.htpasswd;
# Other configurations
}
Limit Access by IP:
location / {
allow 192.168.1.1;
deny all;
# Other configurations
}
Logging
Access Log:
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
# Other configurations
}
Error Log:
http {
error_log /var/log/nginx/error.log;
# Other configurations
}
Additional Tips
- Regularly check and update Nginx configurations.
- Utilize Nginx modules for additional functionalities.
- Explore Nginx documentation for advanced configurations.
This cheat sheet covers some basic Nginx configurations. Adjust the examples based on your specific use case and requirements.