Troubleshoot common Nginx website issues

更新时间:
复制 MD 格式

This topic describes how to troubleshoot common issues with Nginx websites that are deployed on Alibaba Cloud ECS instances.

Details

Note

Note:

  • Before you perform risky operations, such as modifying instance or data configurations, make sure that you have a disaster recovery and fault tolerance plan in place to protect your data.

  • If you modify the configurations or data of instances, such as ECS and RDS, create snapshots or enable features such as RDS log backup in advance.

  • If you have granted permissions or submitted security information, such as logon credentials, on the Alibaba Cloud platform, change them promptly.

Find the troubleshooting method that corresponds to your issue.

Nginx website is inaccessible

Check if Nginx is running

Log on to the server and run the following command to check if ports 80 and 443 are listening.

netstat -nltp |grep -E '80|443'

Run the following command to check if the Nginx process is running.

ps aux |grep nginx #The process name is nginx on CentOS and Alinux systems.

Start the Nginx service

If Nginx is not running, run the following command to start it.

systemctl start nginx.service #For CentOS and Alinux systems

Check security group and firewall rules

  1. Check if the security group rules in the ECS console allow inbound traffic on ports 80 and 443.

    1. Log on to the ECS console.

    2. In the navigation pane on the left, click Instances & Images > Instances.

    3. In the Instance List, find the target ECS instance and click the instance name to open the instance details page.

    4. On the Security Groups tab, check the inbound security rules.

  2. On the server, run the following command to check if any iptables firewall rules are configured.

iptables -nL

4xx/5xx errors when accessing an Nginx website

403 error when accessing an Nginx website

  1. Check the file permissions of the site folder to ensure that the Nginx process has the required read and write permissions.

    To find the site folder, check the information under the `<location>` directive in the Nginx configuration file.

    cat /etc/nginx/nginx.conf

    Run the following command to view the permissions of the site folder /var/www/html/. Replace the path with your actual site folder. The permissions are typically set to 755.

    stat /var/www/html/

    Run the following command to view the permissions of the website files in the site folder. The permissions are typically set to 644.

    ls -l /var/www/html/

2. Check the NGINX configuration file to determine whether access to the site folder is denied.

  • Default configuration files on CentOS and Alinux systems.

    • /etc/nginx/nginx.conf

    • /etc/nginx/vhosts/*.conf

  • Check the <location> section of the configuration file for a Deny *** directive.

  1. Check that the website home page file exists.

    On CentOS and Alinux systems, check the index directive in the <location> block of the nginx.conf configuration file. This directive specifies the default index page, which is typically index.html or index.php.

404 error when accessing an Nginx website

  1. Check whether the site folder and server name are correctly configured in the vhosts section of the Nginx configuration file.

    • Default configuration files on CentOS and Alinux systems:

      • /etc/nginx/nginx.conf

      • /etc/nginx/vhosts/*.conf

  2. Check if the program files exist in the site folder.

  • Check if the home page file exists in the site folder specified by the `root` directive in the configuration file.

  • If you use vhosts to configure multiple sites, check the `root` site folder for each vhost site to ensure that the home page file exists.

5xx error when accessing an Nginx website

  1. Review the Nginx error log and troubleshoot the issue based on the error messages.

    Run the following command to view the error log.

    less /var/log/nginx/error_log #For CentOS and Alinux systems
  2. Check if a rewrite configuration exists in the `<server>` block of the Nginx configuration file and verify that it is correct.

  3. Check if the permissions of the site folder are correct. The permissions are typically set to 755.

    The default site folder is /var/www/html/.

    Run the following command to view the permissions of the /var/www/html/ folder.

    stat /var/www/html/
  4. Enable detailed PHP error logging to view PHP program error messages.

    Modify the php.ini configuration file and add the following configurations to enable detailed log output.

    display_errors = Off
    error_reporting = E_ALL | E_STRICT

    Run the following command to restart Nginx, and then access the PHP page to view the errors.

    systemctl restart nginx #For CentOS and Alinux systems
  5. Troubleshoot common errors.

    For more information about Nginx 502 errors, see Solutions to the "Nginx 502 bad gateway" error and "502" and "connect upstream time out" errors occur when you access a webpage using Nginx.

Troubleshoot slow access to an Nginx website

Check server performance

Check server performance metrics, such as public bandwidth, CPU utilization, and disk input/output operations per second (IOPS).

View the Nginx error log for error messages

Run the following command to check the Nginx error log.

less /var/log/nginx/error_log #For CentOS and Alinux systems

Identify abnormally slow pages

Separately test the performance of static pages and pages that invoke the database. If pages that invoke the database are slow, investigate the database for issues, such as slow SQL statements. If all pages load slowly, check the host's overall load, including its bandwidth, CPU, memory, and disk I/O.

Fix Nginx and PHP runtime exceptions

Check the Nginx service status

  1. Restart the Nginx service.

    systemctl restart nginx    #For CentOS and Alinux systems
  2. Check the Nginx error log and analyze the specific error messages.

    Run the following command to check the Nginx error log.

less /var/log/nginx/error_log #For CentOS and Alinux systems

Troubleshoot PHP runtime exceptions

  1. Enable detailed PHP error messages.

    Modify the php.ini configuration file and add the following configurations to enable detailed log output.

 display_errors = Off
 error_reporting = E_ALL | E_STRICT

Run the following command to restart Nginx.

systemctl restart nginx #For CentOS and Alinux systems
  1. Access the PHP page and troubleshoot the issue based on the detailed PHP error messages.