# 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.

Introduction

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 ```

Symptoms

Common error messages include:

```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 ```

```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 ```

bash
connect() to unix:/run/php/php-fpm.sock failed (11: Resource temporarily unavailable)

Common Causes

  • Configuration misconfiguration
  • Missing or incorrect credentials
  • Network connectivity issues
  • Version compatibility problems
  • Resource exhaustion or limits
  • Permission or access denied

Step-by-Step Fix

  1. 1.Check logs for specific error messages
  2. 2.Verify configuration settings
  3. 3.Test network connectivity
  4. 4.Review recent changes
  5. 5.Apply corrective action
  6. 6.Verify the fix

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. 1.PHP-FPM not running:
  2. 2.```bash
  3. 3.sudo systemctl restart php8.2-fpm
  4. 4.sudo systemctl enable php8.2-fpm
  5. 5.`
  6. 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. 1.Socket permissions:
  2. 2.```bash
  3. 3.# Check socket permissions
  4. 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. 1.Application crashed:
  2. 2.```bash
  3. 3.sudo systemctl restart myapp
  4. 4.`
  5. 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. 1.Buffer issues:
  2. 2.```nginx
  3. 3.location / {
  4. 4.proxy_pass http://localhost:3000;
  5. 5.proxy_buffer_size 128k;
  6. 6.proxy_buffers 4 256k;
  7. 7.proxy_busy_buffers_size 256k;
  8. 8.}
  9. 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. 1.Wrong fastcgi_param:
  2. 2.```nginx
  3. 3.# Wrong
  4. 4.fastcgi_param SCRIPT_FILENAME $fastcgi_script_name;

# Correct fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; ```

  1. 1.Missing fastcgi_params include:
  2. 2.```nginx
  3. 3.location ~ \.php$ {
  4. 4.fastcgi_pass unix:/run/php/php-fpm.sock;
  5. 5.fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
  6. 6.include fastcgi_params; # Don't forget this
  7. 7.}
  8. 8.`
  9. 9.Invalid rewrite rules:
  10. 10.```nginx
  11. 11.# Causes redirect loops
  12. 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. 1.Increase file descriptors:

Edit /etc/security/limits.conf: `` www-data soft nofile 65535 www-data hard nofile 65535

  1. 1.Increase Nginx worker connections:
  2. 2.```nginx
  3. 3.events {
  4. 4.worker_connections 4096;
  5. 5.}
  6. 6.`
  7. 7.Add swap if low memory:
  8. 8.```bash
  9. 9.sudo fallocate -l 2G /swapfile
  10. 10.sudo chmod 600 /swapfile
  11. 11.sudo mkswap /swapfile
  12. 12.sudo swapon /swapfile
  13. 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

  1. 1.Identify the source:
  2. 2.```bash
  3. 3.# Check which component fails
  4. 4.curl -I http://localhost/
  5. 5.`
  6. 6.Check all logs systematically:
  7. 7.```bash
  8. 8.# Nginx
  9. 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. 1.Test configuration:
  2. 2.```bash
  3. 3.sudo nginx -t
  4. 4.sudo php-fpm8.2 -t
  5. 5.`
  6. 6.Restart services:
  7. 7.```bash
  8. 8.sudo systemctl restart nginx
  9. 9.sudo systemctl restart php8.2-fpm
  10. 10.`

Verification

Error PatternCauseFix
Resource temporarily unavailablePHP-FPM pool exhaustedIncrease pm.max_children
Connection reset by peerApplication crashRestart and check app logs
Connection refusedUpstream not runningStart the upstream service
SCRIPT_FILENAME errorsWrong FastCGI paramInclude $document_root
out of memoryResource exhaustionAdd memory or fix leaks
Permission deniedFile permissionschown/chmod files

The 500 error requires investigation. Start with logs, identify which component failed, then apply the specific fix.

  • [Nginx troubleshooting: Fix Lambda Permission Denied - Complete ](fix-lambda-permission-denied)
  • [Nginx web server troubleshooting: Fix Client Max Body Size Large Upload Nginx Issue ](client-max-body-size-large-upload-nginx)
  • [Fix Apache 502 Proxy Error](fix-apache-502-proxy-error)
  • [Fix Apache LogLevel Core Debug Configuration](fix-apache-loglevel-core-debug)
  • [Fix Cloudflare 502 Bad Gateway Error](fix-cloudflare-502-bad-gateway)

<script type="application/ld+json"> { "@context": "https://schema.org", "@type": "TechArticle", "headline": "How to Fix Nginx 500 Internal Server Error", "description": "Resolve Nginx 500 Internal Server Error. Debug PHP-FPM, upstream failures, configuration errors, and resource exhaustion.", "url": "https://www.fixwikihub.com/fix-nginx-500-internal-error", "publisher": { "@type": "Organization", "name": "FixWikiHub", "url": "https://www.fixwikihub.com" }, "author": { "@type": "Person", "name": "FixWikiHub Editorial Team" }, "datePublished": "2025-11-27T20:30:30.823Z", "dateModified": "2025-11-27T20:30:30.823Z" } </script>