SoFunction
Updated on 2025-04-07

Nginx+PHP8.0 supports video upload project practice

Configuring the Nginx + PHP 8.0 architecture on Ubuntu 20.04 to support video uploads requires configuration from both Nginx and PHP to ensure that the server can handle large file uploads. Here are the detailed steps:

1. Nginx configuration

1.1 Increase the upload file size limit

By default, Nginx has restrictions on the size of uploaded files. You need to add this limit to allow uploading larger files, such as video files:

http {
    ...
    client_max_body_size 1G;  # Set the maximum upload file size to 1GB    ...
}

1.2 Configure timeout

Uploading large files may take more time, so you need to increase the relevant timeout:

http {
    ...
    client_body_timeout 120s;  # Set the upload timeout to 120 seconds    send_timeout 120s;         # Set the send timeout time to 120 seconds    keepalive_timeout 120s;    # Set the timeout for keeping connection to 120 seconds    ...
}

1.3 Adjust the buffer size

The size of the buffer affects the performance of file uploads. These buffer sizes can be adjusted according to the server's resources and file size:

http {
    ...
    client_body_buffer_size 256k;   # Adjust the buffer size of the request body    client_header_buffer_size 1k;   # Resize the buffer size of the request header    large_client_header_buffers 4 32k; # Resize the buffer size of the large request header    ...
}

1.4 Enable chunked transfer

For very large files, using chunked transfers can optimize the upload process:

http {
    ...
    chunked_transfer_encoding on;    # Enable block transfer encoding    ...
}

2. PHP configuration

2.1 Add upload_max_filesize and post_max_size

In PHP, you need to configureupload_max_filesizeandpost_max_sizeTo support large file uploads:

Open the PHP configuration file (usually located in/etc/php/8.0/fpm/or/etc/php/8.0/cli/), find and modify the following parameters:

upload_max_filesize = 1G   # Set the maximum upload file size to 1GBpost_max_size = 1G         # Set the maximum POST data size to 1GB

2.2 Add max_execution_time and max_input_time

Make sure that the PHP script has enough time to process uploaded files:

max_execution_time = 300    # Set the maximum execution time of the script to 300 secondsmax_input_time = 300        # Set the maximum processing time of input data to 300 seconds

2.3 Addmemory_limit

To ensure that PHP has enough memory to handle uploaded large files, you can addmemory_limit

memory_limit = 512M         # Set the maximum memory that the script can use is 512MB

3. Connection configuration between Nginx and PHP-FPM

Make sure Nginx is correctly configured to handle PHP requests using PHP-FPM. Common configuration examples:

server {
    listen 80;
    server_name ;

    root /var/www/html;
    index   ;

    location / {
        try_files $uri $uri/ /?$query_string;
    }

    location ~ \.php$ {
        include snippets/;
        fastcgi_pass unix:/var/run/php/php8.;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.ht {
        deny all;
    }
}

4. Restart the service

After completing the above configuration, you need to restart the Nginx and PHP-FPM services to make the configuration take effect:

sudo systemctl restart nginx
sudo systemctl restart php8.0-fpm

5. Check the upload directory permissions

Make sure the uploaded directory has appropriate write permissions so that PHP can save the uploaded files:

sudo chown -R www-data:www-data /path/to/upload/directory
sudo chmod -R 755 /path/to/upload/directory

6. Test the upload function

Make sure everything works properly by performing video upload tests in your project.

The above configuration is still valid when using HTTPS, but you need to make additional configurations to ensure smooth and secure upload and download processes in the HTTPS environment. The following are HTTPS-related configurations that need to be paid attention to:

1. SSL certificate configuration

Make sure your Nginx server has a valid SSL certificate configured and that HTTPS is working properly. Usually, you will define SSL-related parameters in Nginx's configuration file:

server {
    listen 443 ssl;
    server_name ;

    ssl_certificate /etc/nginx/ssl/;
    ssl_certificate_key /etc/nginx/ssl/;

    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_prefer_server_ciphers on;
    ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384';
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 10m;

    ...
}

2. Increase the buffer size of HTTPS

When using HTTPS, data transfers are encrypted and may result in higher resource consumption. You can adjust the following configuration to optimize large file transfers under HTTPS:

server {
    ...
    ssl_buffer_size 1400;  # Adjust the SSL buffer size to adapt to larger file transfers    ...
}

3. Optimize the timeout under HTTPS

To handle large file uploads, especially under HTTPS, make sure the timeout is configured properly:

server {
    ...
    client_body_timeout 120s;  # Upload timeout    send_timeout 120s;         # Send timeout time    keepalive_timeout 120s;    # Keep connection timeout    ...
}

4. Enable HTTP/2 (optional)

HTTP/2 provides a more efficient way to transfer data, especially under HTTPS. You can optimize upload and download performance by enabling HTTP/2 in your Nginx configuration:

server {
    listen 443 ssl http2;  # Enable HTTP/2 support    ...
}

5. Make sure the upload configuration is still valid

The above upload-related configurations (such asclient_max_body_sizeand PHPupload_max_filesizeetc.) Still valid in HTTPS environment. You don't need to modify these configurations, just make sure they are compatible with the HTTPS configuration.

6. Firewall and security configuration

Make sure the firewall allows HTTPS traffic (usually TCP 443 port) and there are no limits on traffic that may affect uploads. You can also enable Nginx's firewall module or other security measures to enhance security of HTTPS uploads.

7. Test video upload under HTTPS

After the configuration is complete, test your upload function via HTTPS to ensure that file upload, transfer speed and security are as expected.

8. Restart the service

After confirming that all configurations are correct again, restart the Nginx and PHP-FPM services:

sudo systemctl restart nginx
sudo systemctl restart php8.0-fpm

Summarize

In an HTTPS environment, the above upload configuration still applies. You just need to add and optimize the SSL configuration in Nginx, consider enabling HTTP/2, and ensure that other HTTPS-related settings are properly configured. Through these configurations, the server should be able to handle video upload tasks safely and efficiently.

This is the article about Nginx+PHP8.0's project practice supporting video upload. For more information about Nginx PHP video upload, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!