在 ubuntu 20.04 上配置 nginx + php 8.0 的架构以支持视频上传,需要从 nginx 和 php 两个方面进行配置,以确保服务器能够处理大文件上传。以下是详细的步骤:
1. nginx 配置
1.1 增加上传文件大小限制
默认情况下,nginx 对上传文件的大小有限制。你需要增加这个限制来允许上传更大的文件,例如视频文件:
http { ... client_max_body_size 1g; # 设置最大上传文件大小为1gb ... }
1.2 配置超时时间
上传大文件可能需要更多的时间,因此你需要增加相关的超时时间:
http { ... client_body_timeout 120s; # 设置上传超时时间为120秒 send_timeout 120s; # 设置发送超时时间为120秒 keepalive_timeout 120s; # 设置保持连接超时时间为120秒 ... }
1.3 调整缓冲区大小
缓冲区的大小影响文件上传的性能,可以根据服务器的资源和文件大小调整这些缓冲区大小:
http { ... client_body_buffer_size 256k; # 调整请求体的缓冲区大小 client_header_buffer_size 1k; # 调整请求头的缓冲区大小 large_client_header_buffers 4 32k; # 调整大请求头的缓冲区大小 ... }
1.4 启用分块传输
对于非常大的文件,使用分块传输可以优化上传过程:
http { ... chunked_transfer_encoding on; # 启用分块传输编码 ... }
2. php 配置
2.1 增加 upload_max_filesize 和 post_max_size
在 php 中,你需要配置 upload_max_filesize
和 post_max_size
来支持大文件上传:
打开 php 配置文件(通常位于 /etc/php/8.0/fpm/php.ini
或 /etc/php/8.0/cli/php.ini
),找到并修改以下参数:
upload_max_filesize = 1g # 设置最大上传文件大小为1gb post_max_size = 1g # 设置最大post数据大小为1gb
2.2 增加 max_execution_time 和 max_input_time
确保 php 脚本有足够的时间来处理上传的文件:
max_execution_time = 300 # 设置脚本最大执行时间为300秒 max_input_time = 300 # 设置输入数据的最大处理时间为300秒
2.3 增加 memory_limit
为了确保 php 有足够的内存来处理上传的大文件,你可以增加 memory_limit
:
memory_limit = 512m # 设置脚本可使用的最大内存为512mb
3. nginx 与 php-fpm 的连接配置
确保 nginx 正确配置为使用 php-fpm 处理 php 请求。常见的配置示例:
server { listen 80; server_name example.com; root /var/www/html; index index.php index.html index.htm; location / { try_files $uri $uri/ /index.php?$query_string; } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/var/run/php/php8.0-fpm.sock; fastcgi_param script_filename $document_root$fastcgi_script_name; include fastcgi_params; } location ~ /\.ht { deny all; } }
4. 重启服务
在完成以上配置后,需要重启 nginx 和 php-fpm 服务以使配置生效:
sudo systemctl restart nginx sudo systemctl restart php8.0-fpm
5. 检查上传目录权限
确保上传的目录有适当的写入权限,以便 php 能够保存上传的文件:
sudo chown -r www-data:www-data /path/to/upload/directory sudo chmod -r 755 /path/to/upload/directory
6. 测试上传功能
通过在你的项目中进行视频上传测试,确保一切工作正常。
在使用 https 的情况下,上述配置仍然有效,但你需要进行额外的配置来确保 https 环境下的上传和下载过程顺畅、安全。以下是需要注意的 https 相关配置:
1. ssl 证书配置
确保你的 nginx 服务器已经配置了有效的 ssl 证书,并且 https 已正常工作。通常,你会在 nginx 的配置文件中定义 ssl 相关的参数:
server { listen 443 ssl; server_name example.com; ssl_certificate /etc/nginx/ssl/example.com.crt; ssl_certificate_key /etc/nginx/ssl/example.com.key; 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. 增加 https 的缓冲区大小
当使用 https 时,数据传输是加密的,可能会导致更高的资源消耗。你可以调整以下配置以优化 https 下的大文件传输:
server { ... ssl_buffer_size 1400; # 调整 ssl 缓冲区大小,适应较大文件传输 ... }
3. 优化 https 下的超时时间
为了处理大文件上传,尤其是在 https 下,确保超时时间配置适当:
server { ... client_body_timeout 120s; # 上传超时时间 send_timeout 120s; # 发送超时时间 keepalive_timeout 120s; # 保持连接超时时间 ... }
4. 启用 http/2(可选)
http/2 提供了更高效的数据传输方式,特别是在 https 下。你可以通过在 nginx 配置中启用 http/2 来优化上传和下载性能:
server { listen 443 ssl http2; # 启用 http/2 支持 ... }
5. 确保上传配置仍然有效
上述与上传相关的配置(如 client_max_body_size
和 php 的 upload_max_filesize
等)在 https 环境下依然有效。你不需要修改这些配置,只需确保它们与 https 配置兼容。
6. 防火墙和安全配置
确保防火墙允许 https 流量(通常是 tcp 443 端口),并且没有限制可能影响上传的流量。你还可以启用 nginx 的防火墙模块或其他安全措施以增强 https 上传的安全性。
7. 测试 https 下的视频上传
完成配置后,通过 https 测试你的上传功能,确保文件上传、传输速度和安全性都符合预期。
8. 重启服务
再次确认所有配置无误后,重启 nginx 和 php-fpm 服务:
sudo systemctl restart nginx sudo systemctl restart php8.0-fpm
总结
在 https 环境下,上述的上传配置仍然适用,你只需在 nginx 中添加和优化 ssl 配置、考虑启用 http/2 并确保其他 https 相关设置都适当配置。通过这些配置,服务器应该能够安全、高效地处理视频上传任务。
到此这篇关于nginx+php8.0支持视频上传的项目实践的文章就介绍到这了,更多相关nginx php视频上传内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论