Troubleshooting of nginx errors

From LemonWiki共筆
Revision as of 16:43, 18 April 2025 by Planetoid (talk | contribs) (→‎Common Permission Issues)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Troubleshooting of Nginx Errors


Troubleshooting of Nginx Errors

Nginx and Apache Server Conflict Troubleshooting Guide

Problem Overview

When trying to access files through Nginx (such as http://example.com/example_folder/example.png), you encounter a 404 error, even though the file definitely exists on the server.

Environment Overview

  • Operating System: Debian
  • Web Server: Nginx and Apache installed simultaneously
  • File Path: /usr/share/nginx/html/example_folder/example.png

Investigation of Issue

1. Verify File Existence

= Confirm the file exists in the correct location on the server =
ls -lht /usr/share/nginx/html/example_folder/

Output shows the file exists:

total 628K
* rw-r--r-- 1 username username 501K Apr 18 12:14 example.png

2. Check And Fix Nginx Configuration

Check the Nginx configuration file syntax for any errors

sudo nginx -t

3. Try Restarting Nginx

sudo systemctl restart nginx

Encountered error:

Job for nginx.service failed because the control process exited with error code.

4. Diagnose Startup Failure Reason

Check service status:

systemctl status nginx.service

Error message:

nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)

5. Check Port Usage

sudo lsof -i :80

Output shows Apache is using port 80:

COMMAND    PID     USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
apache2   1447     root    4u  IPv6  25157      0t0  TCP *:http (LISTEN)
apache2  72832 www-data    4u  IPv6  25157      0t0  TCP *:http (LISTEN)
...

Solutions

Disable Apache, Enable Nginx

# Stop and disable Apache
sudo systemctl stop apache2
sudo systemctl disable apache2

# Start and enable Nginx
sudo systemctl start nginx
sudo systemctl enable nginx


Verify Configuration

After restarting Nginx, try accessing your file:

http://example.com/example_folder/example.png

Common Permission Issues

If you still have access problems, check file permissions: Ensure Nginx user can access the directory

sudo chown -R www-data:www-data /usr/share/nginx/html/example_folder
sudo chmod -R 755 /usr/share/nginx/html/example_folder

Important Notes

  • Nginx and Apache cannot use the same port simultaneously
  • Configuration changes require restarting the Nginx service
  • When checking permission issues, focus on file owners and access permissions
  • Use nginx -t to test if configuration changes are valid