当试图将POST请求发送到Nginx服务器上的静态文件时,HTTP2的行为很奇怪。立即获得200个响应,而无需在HTTP2上发送整个数据并上传整个文件,而不会出现HTTP1.1上的任何问题。
当我将上传路径更改为"upload.php“时,在HTTP2和1.1上一切正常工作。当我们试图将POST请求发送到"upload.bin“或”上载“带有或没有HTTP2扩展名的静态文件时,就会出现问题。
<body>
<input type="file" id="file-input" />
<button id="upload-button">Upload File</button>
<div id=live></div>
<div id=live2></div>
<script type="text/javascript">
var live = document.getElementById('live');
var live2 = document.getElementById('live2');
document.querySelector('#upload-button').addEventListener('click', function() {
if(document.querySelector('#file-input').files.length == 0) {
alert('Error : No file selected');
return;
}
let file = document.querySelector('#file-input').files[0];
let allowed_mime_types = [ 'application/zip', 'image/png' ];
let data = new FormData();
data.append('file', document.querySelector('#file-input').files[0]);
let request = new XMLHttpRequest();
request.open('POST', 'upload');
request.upload.addEventListener("progress", progressHandler, function(e) {
});
request.send(data);
function progressHandler(event){
live.innerHTML = "Event.Loaded = " + event.loaded;
var percent = (event.loaded / event.total)*100;
live2.innerHTML = "Progress percent = " + percent;
}
});在中处理post到静态文件。
error_page 405 =200 $uri;为什么NGINX对HTTP2的POST请求有这样的反应?
发布于 2021-03-18 11:58:37
这是故意的。
NGINX知道405错误的渲染器不需要任何主体,所以对于HTTP,它会丢弃任何接收到的数据。HTTP1.1就是这样设计的。
使用HTTP2,它变得更智能,它告诉另一方中止发送数据,并发送结果页面。这样做是为了防止将被丢弃的数据浪费在互联网数据包上。
HTTP2和higher的工作方式更聪明,在已知的事情上浪费更少的数据(例如,如果您需要登录文件上传,它只会尽快告诉客户端有错误,而等到完整的文件已经上传)。
当您向.php文件发送请求时,php进程接管它,并且它无法立即返回结果,因此NGINX在显示错误之前将整个页面流到php,因为PHP只在收到文件后才开始在页面上执行代码,并且它可能会对POST请求执行一些操作。
https://stackoverflow.com/questions/66687022
复制相似问题