HxHippy

Testing Nginx Configuration

How to test and validate your Nginx configuration before applying changes.

Last updated: 2025-01-15

Configuration Testing Commands

Basic Syntax Check

# Test configuration syntax
nginx -t

# Expected output:
# nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
# nginx: configuration file /etc/nginx/nginx.conf test is successful

Show Full Configuration

# Display parsed configuration (includes all includes)
nginx -T

# Useful for debugging include paths
nginx -T 2>&1 | head -50

Specify Configuration File

# Test a specific config file
nginx -t -c /path/to/custom/nginx.conf

# Test config in non-standard location
nginx -t -c /etc/nginx/sites-available/example.com

Debugging Techniques

Check Running Configuration

# View master process config
cat /proc/$(cat /run/nginx.pid)/cmdline | tr '\0' ' '

# Check which config file is loaded
nginx -V 2>&1 | grep 'configure arguments'

Enable Debug Logging

# In nginx.conf - set error log to debug level
error_log /var/log/nginx/error.log debug;

# Debug specific connection (by IP)
events {
    debug_connection 192.168.1.100;
}

Access Log Debugging

# Detailed log format
log_format debug '$remote_addr - $remote_user [$time_local] '
                 '"$request" $status $body_bytes_sent '
                 '"$http_referer" "$http_user_agent" '
                 'rt=$request_time uct="$upstream_connect_time" '
                 'uht="$upstream_header_time" urt="$upstream_response_time"';

access_log /var/log/nginx/debug.log debug;

Common Testing Scenarios

Test Before Reload

# Always test before reload
nginx -t && systemctl reload nginx

# Safe deployment script
#!/bin/bash
nginx -t
if [ $? -eq 0 ]; then
    systemctl reload nginx
    echo "Configuration reloaded successfully"
else
    echo "Configuration test failed, not reloading"
    exit 1
fi

Compare Configurations

# Backup current config
cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.backup

# Compare after changes
diff /etc/nginx/nginx.conf /etc/nginx/nginx.conf.backup

Validate SSL Certificates

# Check certificate details
openssl x509 -in /etc/nginx/ssl/cert.pem -text -noout

# Verify certificate chain
openssl verify -CAfile /etc/nginx/ssl/chain.pem /etc/nginx/ssl/cert.pem

# Test SSL configuration
openssl s_client -connect example.com:443 -servername example.com

Troubleshooting Common Errors

Address Already in Use

# Find what's using port 80
ss -tlnp | grep :80
lsof -i :80

# Kill process if needed
kill -9 $(lsof -t -i :80)

Permission Denied

# Check file permissions
ls -la /var/www/html
namei -l /var/www/html/index.html

# Check nginx user
grep 'user' /etc/nginx/nginx.conf

# Fix permissions
chown -R nginx:nginx /var/www/html
chmod -R 755 /var/www/html

Could Not Open Error Log

# Create log directory
mkdir -p /var/log/nginx
chown nginx:nginx /var/log/nginx

# Check SELinux (if applicable)
restorecon -Rv /var/log/nginx
beginner Getting Started Updated 2025-01-15
  • nginx
  • testing
  • validation
  • debug
  • syntax check