Nginx FAQ

Installation Issues

What should I do if Nginx installation fails?

  1. Check if system dependencies are complete
  2. Check error logs to understand the specific failure reason
  3. Ensure sufficient disk space
  4. Check for permission issues

How to check Nginx version?

nginx -v
nginx -V  # Detailed information including compilation parameters

What operating systems does Nginx support?

Nginx supports Linux, FreeBSD, Solaris, AIX, HP-UX, macOS, and Windows.

Configuration Issues

How to check if Nginx configuration is correct?

nginx -t

Where is the configuration file?

  • Debian/Ubuntu: /etc/nginx/nginx.conf
  • CentOS/RHEL: /etc/nginx/nginx.conf
  • Compiled installation: /usr/local/nginx/conf/nginx.conf

Do I need to restart after modifying configuration?

No restart needed. Use nginx -s reload to reload configuration.

Performance Issues

How to optimize Nginx performance?

  1. Adjust worker process count
worker_processes auto;
  1. Increase worker connections
events {
    worker_connections 4096;
}
  1. Enable gzip compression
gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml;
  1. Enable browser caching
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
    expires 30d;
}
  1. Use connection pooling
upstream backend {
    server backend1.example.com;
    keepalive 32;
}

What to do if Nginx consumes too much memory?

  1. Reduce worker_connections number
  2. Limit request body size
  3. Optimize cache configuration
  4. Check for memory leaks

How to handle high concurrency?

  1. Use load balancing
  2. Enable caching
  3. Adjust kernel parameters
  4. Use CDN

Security Issues

How to prevent DDoS attacks?

  1. Limit connections
limit_conn_zone $binary_remote_addr zone=addr:10m;
  1. Limit request rate
limit_req_zone $binary_remote_addr zone=one:10m rate=10r/s;
  1. Close slow connections
client_body_timeout 5s;
client_header_timeout 5s;

How to configure SSL certificate?

server {
    listen 443 ssl http2;
    ssl_certificate /path/to/cert.crt;
    ssl_certificate_key /path/to/cert.key;
    ssl_protocols TLSv1.2 TLSv1.3;
}

How to force HTTPS?

server {
    listen 80;
    server_name example.com;
    return 301 https://$server_name$request_uri;
}

Proxy Issues

What to do if reverse proxy fails?

  1. Check if backend service is running
  2. Check firewall settings
  3. Verify proxy_pass configuration is correct
  4. Check error logs

How to proxy WebSocket?

location /ws/ {
    proxy_pass http://backend;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
}

How to get real IP after proxy?

proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

Log Issues

What to do if log files are too large?

Use logrotate for log rotation:

# /etc/logrotate.d/nginx
/var/log/nginx/*.log {
    daily
    rotate 14
    compress
    notifempty
}

How to disable access logging?

access_log off;

How to configure log format?

log_format main '$remote_addr - $remote_user [$time_local] "$request" '
                '$status $body_bytes_sent "$http_referer" '
                '"$http_user_agent"';

Load Balancing Issues

How does load balancing select backend?

Nginx supports multiple load balancing algorithms:

  • Round-robin (default): Distributes requests sequentially
  • Weighted round-robin: Distributes requests based on weight
  • IP hash: Routes requests from same IP to same backend
  • Least connections: Routes to backend with fewest connections

What if backend server goes down?

Nginx automatically skips failed nodes and forwards requests to healthy ones.

Cache Issues

How to configure proxy caching?

proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=my_cache:10m max_size=100m inactive=60m;

location / {
    proxy_cache my_cache;
    proxy_cache_valid 200 60m;
    proxy_pass http://backend;
}

What to do if caching doesn't work?

  1. Check cache path permissions
  2. Confirm response headers allow caching
  3. Verify cache key configuration is correct

How to clear cache?

rm -rf /var/cache/nginx/*

Port Issues

What if port is already in use?

# Check port usage
netstat -tulpn | grep :80

# Use different port
listen 8080;

How to listen on multiple ports?

server {
    listen 80;
    listen 8080;
    server_name example.com;
}

Common Error Codes

400 Bad Request

  • Request header too large: Increase client_header_buffer_size
  • URL too long: Check request path

403 Forbidden

  • Index file does not exist
  • Insufficient permissions: Check file and directory permissions

404 Not Found

  • File path error
  • Blocked by SELinux or AppArmor

502 Bad Gateway

  • Backend service not running
  • FastCGI/PHP-FPM configuration error

504 Gateway Timeout

  • Backend response timeout
  • Increase proxy_read_timeout

Debugging Tips

How to view detailed error information?

  1. Enable debug logging:
error_log /var/log/nginx/error.log debug;
  1. Check error logs:
tail -f /var/log/nginx/error.log

How to test if configuration takes effect?

# Test configuration
nginx -t

# Display full configuration
nginx -T

How to debug regular expressions?

Use location testing:

location /test {
    access_log /var/log/nginx/test.log;
}

Upgrade Issues

How to upgrade Nginx?

  1. Backup configuration files
  2. Stop Nginx
  3. Install new version
  4. Start Nginx
  5. Test functionality

How to perform smooth upgrade?

# Compile new version
make

# Send USR2 signal to upgrade
kill -s USR2 $(cat /var/run/nginx.pid)

# Gracefully shut down old worker processes
kill -s WINCH $(cat /var/run/nginx.pid.oldbin)