Signup/Sign In
PUBLISHED ON: MARCH 10, 2023

Limit file uploading speed in Nginx

In the previous tutorial, we learnt how we can limit the file download speed in Nginx which was pretty straightforward. Now, This tutorial explains the steps used to limit file upload speed to Nginx reverse proxy to the upload API.

How to add a domain to nginx?

The default configuration file for Nginx is /etc/nginx/nginx.conf. You can add domains to this configuration but it will become harder to manage. So, it is recommended to add another configuration file (in /etc/nginx/sites-available directory) and add a new domain to it.

1. Start by creating a new configuration file in sites available directory. Name the configuration file with the domain name in it for quick identification.

$ sudo touch /etc/nginx/sites-available/domain.com.conf

2. Open this configuration file in a text editor.

$ sudo vi /etc/nginx/sites-available/domain.com.conf

How to limit file upload speed in Nginx server?

Stream block is required to define single/multiple file upload API and then nginx should be used as a reverse proxy to handle the upload rate.

Here,

  • There are two upload APIs running on port 8080, defined in upstream block, and
  • In the server block inside stream block, It is listening on port 55555 (reverse proxy).
  • In the http=>server block, we can see that the requests made to the /upload endpoint is forwarded to the port 55555.
user www-data;
worker_processes auto;
pid /run/nginx.pid;

events {
    worker_connections 1;
}

stream {
    upstream site {
        server upload-api.domain1.com:8080;
        server upload-api.domain2.com:8080;
    }

    server {
        listen    55555;
        proxy_upload_rate 256k;
        proxy_pass site;
    }
}

http {
  server {

    location = /upload { 
        proxy_request_buffering off;   
        proxy_pass http://127.0.0.1:55555/upload?$args;
    }

  }
}

Finally, save the configuration file and exit the text editor and start testing the new configuration.

Verify our configuration, then launch Nginx.

It is a recommended practice to test new configurations before loading them to the production server because a single syntax mistake will prevent the Nginx service from running, which will prevent users from accessing your website. You can use the following command to do so.

$ sudo nginx -t -c /etc/nginx/sites-available/domain.com.conf

Enable the site if no issues were discovered. In order to accomplish this, we must build a symbolic link between the site configuration file and the sites-enabled directory using the following command:

$ sudo ln -s /etc/nginx/sites-available/domain.com.conf /etc/nginx/sites-enabled/domain.com.conf

Finally, Start/Restart/Reload the server to load the changes:

$ sudo systemctl restart nginx

or

$ sudo service nginx restart



About the author:
Pradeep has expertise in Linux, Go, Nginx, Apache, CyberSecurity, AppSec and various other technical areas. He has contributed to numerous publications and websites, providing his readers with insightful and informative content.