# Nginx 500 Internal Server Error
The 500 Internal Server Error is Nginx's way of saying "something went wrong, but I won't tell you what." It's the catch-all error that requires investigation. The error is rarely Nginx itself - it's almost always the upstream application, PHP processor, or a misconfiguration.
Understanding the Error
A 500 error from Nginx means the request reached the server but processing failed. Unlike 502 (bad gateway) or 503 (unavailable), a 500 typically indicates the application crashed, misconfigured, or hit an internal error.
Check both Nginx and application logs:
```bash # Nginx error log tail -f /var/log/nginx/error.log
# Application logs (location varies) tail -f /var/log/php-fpm/error.log tail -f /var/www/html/storage/logs/laravel.log tail -f /var/log/nodejs/app.log ```
Common Cause 1: PHP-FPM Issues
For PHP applications, PHP-FPM is the most common source of 500 errors.
Diagnosis:
```bash # Check PHP-FPM status sudo systemctl status php8.2-fpm
# Check PHP-FPM logs tail -f /var/log/php-fpm/error.log
# Test PHP-FPM socket connectivity ls -la /run/php/php-fpm.sock ```
Typical error messages:
``
connect() to unix:/run/php/php-fpm.sock failed (11: Resource temporarily unavailable)
recv() failed (104: Connection reset by peer) while reading response header from upstream
Solutions:
- 1.PHP-FPM not running:
- 2.```bash
- 3.sudo systemctl restart php8.2-fpm
- 4.sudo systemctl enable php8.2-fpm
- 5.
` - 6.Pool exhausted (too many connections):
Edit /etc/php/8.2/fpm/pool.d/www.conf:
``ini
pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 500
- 1.Socket permissions:
- 2.```bash
- 3.# Check socket permissions
- 4.ls -la /run/php/php-fpm.sock
# Fix ownership sudo chown www-data:www-data /run/php/php-fpm.sock ```
Or use TCP instead of socket in pool config:
``ini
listen = 127.0.0.1:9000
Update Nginx config:
``nginx
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
Common Cause 2: Upstream Application Error
When Nginx proxies to an application (Node.js, Python, Go, etc.), application crashes cause 500 errors.
Diagnosis:
```bash # Check if upstream is running curl http://localhost:3000/health
# Check upstream process ps aux | grep node ps aux | grep gunicorn ps aux | grep uwsgi
# Check upstream logs journalctl -u myapp -f ```
Nginx error log:
``
upstream prematurely closed connection while reading response header from upstream
Solutions:
- 1.Application crashed:
- 2.```bash
- 3.sudo systemctl restart myapp
- 4.
` - 5.Timeout issues:
Increase timeouts in Nginx:
``nginx
location / {
proxy_pass http://localhost:3000;
proxy_connect_timeout 60s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
}
- 1.Buffer issues:
- 2.```nginx
- 3.location / {
- 4.proxy_pass http://localhost:3000;
- 5.proxy_buffer_size 128k;
- 6.proxy_buffers 4 256k;
- 7.proxy_busy_buffers_size 256k;
- 8.}
- 9.
`
Common Cause 3: Nginx Configuration Errors
Some configuration errors cause 500 errors rather than failing configuration test.
Check configuration:
``bash
sudo nginx -t
Common issues:
- 1.Wrong fastcgi_param:
- 2.```nginx
- 3.# Wrong
- 4.fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;
# Correct fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; ```
- 1.Missing fastcgi_params include:
- 2.```nginx
- 3.location ~ \.php$ {
- 4.fastcgi_pass unix:/run/php/php-fpm.sock;
- 5.fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
- 6.include fastcgi_params; # Don't forget this
- 7.}
- 8.
` - 9.Invalid rewrite rules:
- 10.```nginx
- 11.# Causes redirect loops
- 12.rewrite ^(.*)$ $1?args last;
# Fix: be more specific rewrite ^/old/(.*)$ /new/$1 permanent; ```
Common Cause 4: Resource Exhaustion
Server running out of memory or file descriptors causes 500 errors.
Diagnosis:
```bash # Check memory free -m
# Check disk space df -h
# Check file descriptors cat /proc/sys/fs/file-nr
# Check Nginx worker connections grep worker_connections /etc/nginx/nginx.conf ```
Check system logs:
``bash
dmesg | tail -20
journalctl -xe | grep -i "out of memory"
Solutions:
- 1.Increase file descriptors:
Edit /etc/security/limits.conf:
``
www-data soft nofile 65535
www-data hard nofile 65535
- 1.Increase Nginx worker connections:
- 2.```nginx
- 3.events {
- 4.worker_connections 4096;
- 5.}
- 6.
` - 7.Add swap if low memory:
- 8.```bash
- 9.sudo fallocate -l 2G /swapfile
- 10.sudo chmod 600 /swapfile
- 11.sudo mkswap /swapfile
- 12.sudo swapon /swapfile
- 13.
`
Common Cause 5: Permissions on Application Files
The application can't read its own files.
Diagnosis: ```bash # Check as web server user sudo -u www-data ls /var/www/html/config.php sudo -u www-data ls /var/www/html/storage/
# Check permissions ls -la /var/www/html/ ```
Solution: ```bash # Fix ownership sudo chown -R www-data:www-data /var/www/html/
# Fix permissions find /var/www/html -type d -exec chmod 755 {} \; find /var/www/html -type f -exec chmod 644 {} \;
# Writable directories chmod -R 775 /var/www/html/storage/ chmod -R 775 /var/www/html/bootstrap/cache/ ```
Common Cause 6: PHP Fatal Errors
PHP syntax errors, missing extensions, or fatal runtime errors.
Diagnosis: ```bash # Enable PHP error display temporarily echo "error_reporting = E_ALL" | sudo tee /etc/php/8.2/fpm/conf.d/99-debug.ini echo "display_errors = On" | sudo tee -a /etc/php/8.2/fpm/conf.d/99-debug.ini
sudo systemctl restart php8.2-fpm
# Check for errors curl http://localhost/ ```
Check PHP errors in log:
``bash
grep -i "fatal|error" /var/log/php-fpm/error.log
Common PHP errors:
- Allowed memory size of X bytes exhausted - Increase memory_limit
- Maximum execution time exceeded - Increase max_execution_time
- Class 'SomeClass' not found - Missing dependency, run composer install
- Call to undefined function - Missing PHP extension
Verification Steps
- 1.Identify the source:
- 2.```bash
- 3.# Check which component fails
- 4.curl -I http://localhost/
- 5.
` - 6.Check all logs systematically:
- 7.```bash
- 8.# Nginx
- 9.tail -20 /var/log/nginx/error.log
# PHP-FPM tail -20 /var/log/php-fpm/error.log
# Application tail -20 /var/www/html/storage/logs/laravel.log ```
- 1.Test configuration:
- 2.```bash
- 3.sudo nginx -t
- 4.sudo php-fpm8.2 -t
- 5.
` - 6.Restart services:
- 7.```bash
- 8.sudo systemctl restart nginx
- 9.sudo systemctl restart php8.2-fpm
- 10.
`
Quick Reference
| Error Pattern | Cause | Fix |
|---|---|---|
Resource temporarily unavailable | PHP-FPM pool exhausted | Increase pm.max_children |
Connection reset by peer | Application crash | Restart and check app logs |
Connection refused | Upstream not running | Start the upstream service |
SCRIPT_FILENAME errors | Wrong FastCGI param | Include $document_root |
out of memory | Resource exhaustion | Add memory or fix leaks |
Permission denied | File permissions | chown/chmod files |
The 500 error requires investigation. Start with logs, identify which component failed, then apply the specific fix.