Troubleshooting of nginx errors: Difference between revisions

From LemonWiki共筆
Jump to navigation Jump to search
(Created page with "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 <code>http://example.com/example_folder/example.png</code>), 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...")
 
 
(One intermediate revision by the same user not shown)
Line 6: Line 6:
'''Problem Overview'''
'''Problem Overview'''


When trying to access files through Nginx (such as <code>http://example.com/example_folder/example.png</code>), you encounter a 404 error, even though the file definitely exists on the server.
When trying to access files through Nginx (such as {{kbd | key=<nowiki>http://example.com/example_folder/example.png</nowiki>}}), you encounter a 404 error, even though the file definitely exists on the server.


'''Environment Overview'''
'''Environment Overview'''
Line 97: Line 97:
=== Common Permission Issues ===
=== Common Permission Issues ===


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


<pre lang="bash">
<pre lang="bash">
= Ensure Nginx user can access the directory =
sudo chown -R www-data:www-data /usr/share/nginx/html/example_folder
sudo chown -R www-data:www-data /usr/share/nginx/html/example_folder
sudo chmod -R 755 /usr/share/nginx/html/example_folder
sudo chmod -R 755 /usr/share/nginx/html/example_folder

Latest revision as of 16:43, 18 April 2025

Troubleshooting of Nginx Errors


Troubleshooting of Nginx Errors[edit]

Nginx and Apache Server Conflict Troubleshooting Guide[edit]

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[edit]

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[edit]

  • 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