Azure

Azure App Service: NGINX 404 file not found error

In the Azure App Service, when switching from PHP 7 to PHP 8, the operating system changes from Apache to Nginx. The custom rules previously defined, including the rewrite rules in the .htaccess file, will not work for Nginx.

This will break the website/app and lead to an Nginx 404 “file not found” error. To fix the error, we need to set up a custom startup script and modify the existing Nginx configuration for the app.

  1. Go to your App Service in the Azure Portal and click on SSH, which is located under Development Tools.
  2. To modify the default Nginx site configuration, you should make a copy of the existing configuration file and add it to the /home/site directory.
    cp /etc/nginx/sites-available/default /home/site/default
  3. After copying the file, navigate to /home/site/default and edit the file. Update the following code in the file.
    server {
    	...	
    	...
    	...
    	...
    
        location / {
            index  index.php index.html index.htm hostingstart.html;
            try_files $uri $uri/ /index.php?$args;
        }
    	...
    	...
    	...
    	...
    	...
    }
    
  4. Create a custom script named /home/site/startup.sh that overrides the existing /etc/nginx/sites-available/default file with the updated version located in /home/site/default. Once the script is created, reload the Nginx service to ensure that the changes take effect.
    #!/bin/bash
    cp /home/site/default /etc/nginx/sites-available/default
    service nginx reload
    
  5. Navigate back to your App Service on the Azure Portal and go to Settings, then select Configuration, followed by General Settings.In the Startup Command box, add /home/site/startup.sh, then save the settings. After saving the settings, check your application or website to verify that the changes were successful. This should resolve the Nginx 404 “not found” error.
    If your startup.sh is not executed when the web app server is restarted, check if the error exists in startup.sh

    • To check if the error exists in the startup.sh file as a script, execute the startup.sh file as a script, you can use the following startup command:
      bash /home/site/startup.sh

      or

      /home/site/startup.sh
    • If you receive the following error:
      root@300a55b17226:/home# /home/site/startup.sh -bash: /home/site/startup.sh: /bin/bash^M: bad interpreter: No such file or directory root@300a55b17226:/home# ^C root@300a55b17226:/ho

      The error message you received indicates that there is a problem with the script’s interpreter. The ^M characters at the end of the #!/bin/bash line indicate that the script was saved in Windows format and contains carriage return characters.

    • To fix this error install dos2unix

      apt-get install dos2unix
    • Once dos2unix is installed, you can use it to convert the startup.sh script to Unix format by running the following command
      dos2unix /home/site/startup.sh

      This command will remove the carriage return characters from the script and convert it to Unix format.

    • After converting the file to Unix format, you should be able to execute the script using the command:
    • If you encounter any other types of errors, you should diagnose the issue and take appropriate steps to resolve it.
  6. If step number 5 does not resolve the error, remove /home/site/startup.sh from the Startup Command box. Then, add the command that copies the updated Nginx site configuration file from /home/site/default to /etc/nginx/sites-available/default, and then reload the Nginx service in the Startup Command and save the new settings. Essentially, this is the same command used in startup.sh .
    cp /home/site/default /etc/nginx/sites-available/default;
    service nginx reload

  7. If steps  5 and 6 do not resolve the issue, access SSH and execute the commands that are written in the startup.sh file. However, this method has a drawback as you will need to run the commands manually each time you restart your app.

Views: 1651

Standard