nginx 是否允许上游服务器在请求完成之前响应并关闭请求?

the*_*art 6 nginx streaming node.js

我有一个 nginx 代理请求的图像上传服务。一切都很好。但有时,服务器已经拥有用户正在上传的图像。所以,我想早点回应并关闭连接。

在读取标头并与服务器核对后,我调用 Node 的response.end([data][, encoding][, callback])

Nginx barfs 并返回空白响应:

[error] 3831#0: *12879 readv() failed (104: Connection reset by peer) while reading upstream
Run Code Online (Sandbox Code Playgroud)

我的猜测是 nginx 假设上游服务器发生了一些不好的事情,立即断开客户端连接而不发送上游服务器的响应。

有谁知道当 nginx 是代理时如何正确响应和关闭客户端的连接?我知道这是可能的:请参阅:在请求进入之前发送响应

这是nginx conf文件:

worker_processes 8; # the number of processors
worker_rlimit_nofile 128; # each connection needs 2 file handles

events {
  worker_connections 128; # two connections per end-user connection (proxy)
  multi_accept on;
  use kqueue;
}

http {
  sendfile on;
  tcp_nopush on; # attempt to send HTTP response head in one packet
  tcp_nodelay off; # Nagle algorithm, wait until we have the maximum amount of data the network can send at once
  keepalive_timeout 65s;

  include nginx.mime.types;
  default_type application/octet-stream;

  error_log /usr/local/var/log/nginx/error.log;
  log_format main '$remote_addr - $remote_user [$time_local] '
                    '"$request" $status $body_bytes_sent "$http_referer" '
                    '"$http_user_agent" "$http_x_forwarded_for"';

  gzip off;

}

upstream upload_service {
  server 127.0.0.1:1337 fail_timeout=0;
  keepalive 64;
}

location /api/upload_service/ {
  # setup proxy to UpNode
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_set_header X-Forwarded-Proto $scheme;
  proxy_set_header Host $http_host;
  proxy_set_header X-NginX-Proxy true;
  proxy_set_header Connection "";
  proxy_pass http://upload_service;

  # The timeout is set only between two successive read operations
  proxy_read_timeout 500s;
  # timeout for reading client request body, only for a period between two successive read   operations
  client_body_timeout 30s;
  # maximum allowed size of the client request body, specified in the "Content-Length"
  client_max_body_size 64M;
}
Run Code Online (Sandbox Code Playgroud)

Col*_*Cat 2

您没有提到您的客户是什么,但是,这听起来像是您可以通过预期标头实现的目标。本质上,客户端设置了一个带有“100-Continue”期望的“Expect”标头。然后,客户端等待来自服务器的 100 Continue 响应,然后发送其请求正文。

如果服务器不想接收主体,它可以响应最终状态,并且客户端不发送主体。

此过程在RFC2616 第 8.2.3 节中定义