Nginx FAQ
Installation Issues
What should I do if Nginx installation fails?
- Check if system dependencies are complete
- Check error logs to understand the specific failure reason
- Ensure sufficient disk space
- 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?
- Adjust worker process count
worker_processes auto;
- Increase worker connections
events {
worker_connections 4096;
}
- Enable gzip compression
gzip on;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml;
- Enable browser caching
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 30d;
}
- Use connection pooling
upstream backend {
server backend1.example.com;
keepalive 32;
}
What to do if Nginx consumes too much memory?
- Reduce worker_connections number
- Limit request body size
- Optimize cache configuration
- Check for memory leaks
How to handle high concurrency?
- Use load balancing
- Enable caching
- Adjust kernel parameters
- Use CDN
Security Issues
How to prevent DDoS attacks?
- Limit connections
limit_conn_zone $binary_remote_addr zone=addr:10m;
- Limit request rate
limit_req_zone $binary_remote_addr zone=one:10m rate=10r/s;
- 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?
- Check if backend service is running
- Check firewall settings
- Verify proxy_pass configuration is correct
- 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?
- Check cache path permissions
- Confirm response headers allow caching
- 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?
- Enable debug logging:
error_log /var/log/nginx/error.log debug;
- 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?
- Backup configuration files
- Stop Nginx
- Install new version
- Start Nginx
- 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)
