Error 413 - File upload >2MB blocked by Nginx on Azure despite adjusted PHP configuration

Digital - KEPAX 5 Reputation points
2023-11-30T11:10:59.98+00:00

Hello,

I am facing an issue with file uploads to SharePoint through a PHP application using GraphAPI. Locally, I can upload files up to 20MB without any issues after modifying upload_max_filesize and post_max_size to 50M and 60M, respectively. However, in production on Azure, even though phpinfo() confirms that the changes are in place, any file larger than 2MB triggers a "413 Request Entity Too Large" error specific to Nginx.

Additional technical details:

  • Request method: POST
  • Server: nginx/1.24.0
  • Error: 413 Request Entity Too Large
  • PHP settings: upload_max_filesize = 50M, post_max_size = 60M confirmed by phpinfo()
  • Local behavior: Works perfectly
  • Production behavior: Error beyond 2MB

It seems that the Nginx server on Azure is not applying the PHP settings as expected. Are there additional configurations or specific steps on Azure that I need to follow to allow the upload of large files? Or are there file size restrictions for uploads via Nginx that I could adjust?

I am seeking guidance to troubleshoot this limitation, and any assistance would be greatly appreciated.

Thank you in advance for your support.

Best regards,

Azure Files
Azure Files
An Azure service that offers file shares in the cloud.
1,417 questions
Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
13,711 questions
Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
8,914 questions
0 comments No comments
{count} vote

4 answers

Sort by: Most helpful
  1. Deepanshu Sharma 500 Reputation points Microsoft External Staff
    2023-12-02T19:18:59.7733333+00:00

    Hi @Digital - KEPAX

    Welcome to our forum!

    Nginx default value for upload file is 1MB

    You can update this value by three different way by adding ‘client_max_body_size xxM’ inside /etc/nginx/nginx.conf or /etc/nginx/sites-available/default where xx is the size (in megabytes) that you want to allow.

    1. Set in http block which affects all server blocks (virtual hosts).

    http {
        ...
        client_max_body_size 100M;
    }
    

    2. Set in server block, which affects a particular site/app.

    server {
        ...
        client_max_body_size 100M;
    }
    

    3. Set in location block, which affects a particular directory (uploads) under a site/app.

    location /uploads {
        ...
        client_max_body_size 100M;
    }
    

    Save the file and restart Nginx web server to apply the recent changes using following command.

    systemctl restart nginx

    2 people found this answer helpful.

  2. Digital - KEPAX 5 Reputation points
    2023-12-20T12:53:22.7733333+00:00

    Could someone else help us?
    We are stuck, and it's impossible to upload files larger than 2MB, which is very limiting. Please !!

    0 comments No comments

  3. Mike Sweany 0 Reputation points
    2024-01-10T20:11:06.2266667+00:00

    I dealt with this last week and just resolved it.

    I followed info from this link, https://techcommunity.microsoft.com/t5/apps-on-azure-blog/configure-nginx-for-php-8-linux-azure-app-service/ba-p/3069373 here's the details of what I did to get it working.

    After you make the PHP changes, go into the KUDU console and SSH into the App Service.

    Get to this in the web app > development tools > advanced tools > SSH at the top.

    Once in the shell, cd to /etc/nginx and make sure it's there.

    If you do an "ls" and see nginx.conf in there, you are in the right place and right config.

    Copy the default config from nginx to your home directory

    cp /etc/nginx/sites-enabled/default /home/default
    

    cd to your home dir

    cd /home 
    

    while you are here, modify the default file in /home/default

    server {
        client_max_body_size 10m;
    .....
    

    Create the startup script

    nano startup.sh
    

    Add this content to your startup.sh script (from this link, which was in the comments of the link above) https://www.schakko.de/2021/09/08/deploying-php-8-0-applications-with-azure-app-service/

    #!/bin/bash
    echo "Replacing nginx configuration for serving PHP 8.0-based applications"
    cp /home/default /etc/nginx/sites-available/default 
    
    echo "Reloading nginx to apply new configuration"
    service nginx reload
    

    That will copy the new default file over to nginx on startup and for whatever reason, nginx needs to be reloaded. I am at a loss why it doesn't reload with the app service restart.

    Tell the web app the script is executable (I just did this by habit, not sure if it's necessary)

    chmod +x startup.sh
    

    Now, all you need to do is tell the app to run the script at startup

    In your web app > configuration > general settings, add this to your "Startup Command"

    /home/startup.sh
    

    Now, when you restart the web app service, it should launch that script at startup, copy the file over and reload nginx. You can tell it worked if you cd to /etc/nginx/sites-enabled/ and cat default to see the config file. There it should show your new config setting.

    You can also see if it started nginx correctly with

    nginx -t
    

    and finally, this will show the entire nginx config, which should include your modification (the new line at the top of server{})

    nginx -T
    

    I hope this helps someone else!


  4. Pinaki Ghatak 5,600 Reputation points Microsoft Employee Volunteer Moderator
    2024-01-24T15:43:16.8933333+00:00

    Hello @Digital - KEPAX

    The issue seems to be that the PHP settings are not working as expected on the Nginx server on Azure. Here are some things we can do to find out what is causing this issue:

    1. Check the Nginx configuration on Azure: Maybe there are some configurations or restrictions on the Nginx server that are blocking the upload of large files. You can look at the Nginx configuration on Azure to see if there are any limits on file size uploads. You can also look at the error logs on the Nginx server to see if there are any error messages related to the file upload.
    2. Check the Azure configuration: Maybe there are some configurations or restrictions on Azure that are blocking the upload of large files. You can look at the Azure configuration to see if there are any limits on file size uploads. You can also look at the Azure logs to see if there are any error messages related to the file upload. 3.
    3. Check the GraphAPI configuration: Maybe there are some configurations or restrictions on the GraphAPI that are blocking the upload of large files. You can look at the GraphAPI configuration to see if there are any limits on file size uploads. Also, you can try these steps to find out the issue:
    • Try uploading a smaller file: If you haven't done it yet, try uploading a file that is smaller than 2MB to see if it works. This will help you figure out if the issue is related to file size restrictions.
    • Try uploading the same file locally and on Azure: If you haven't done it yet, try uploading the same file locally and on Azure to see if there are any differences in how it works. This will help you figure out if the issue is related to the environment or the code.
    • Check the PHP code: Maybe there are problems with the PHP code that are blocking the upload of large files. You can look at the PHP code to see if there are any problems related to file size uploads. I hope this helps you find out the issue.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.