Java nginx和springrest流(文本/事件流)存在问题
我有一个Spring REST控制器,它是这样的:Java nginx和springrest流(文本/事件流)存在问题,java,spring,rest,nginx,stream,Java,Spring,Rest,Nginx,Stream,我有一个Spring REST控制器,它是这样的: @GetMapping(value="/auth/stream/{param}", produces = MediaType.TEXT_EVENT_STREAM_VALUE) public ResponseEntity<Flux<ServerSentEvent<LoginRequisition>>> userStream(@PathVariable String p
@GetMapping(value="/auth/stream/{param}", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public ResponseEntity<Flux<ServerSentEvent<LoginRequisition>>> userStream(@PathVariable String param) {
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type", "text/event-stream");
headers.add("Cache-Control", "no-cache");
headers.add("X-Accel-Buffering", "no");
return ResponseEntity.ok().headers(headers).body(myStreamManager.DoSomething(param));
}
location /app/ {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://127.0.0.1:8444/;
}
location /app/auth/stream/ {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://127.0.0.1:8444/;
proxy_http_version 1.1;
proxy_set_header Connection "";
}
location /app/ {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://127.0.0.1:8444/;
location /app/auth/stream {
proxy_http_version 1.1;
proxy_set_header Connection "";
}
}
在添加最后两行之后,一切都开始正常工作:
location /app/ {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://127.0.0.1:8444/;
#new lines
proxy_http_version 1.1;
proxy_set_header Connection "";
}
但是这个位置/app/适用于所有其他端点,我不需要将它们配置为长时间运行的http连接,所以我尝试仅将此规则定义到我的特定端点,这就造成了混乱
首先,我尝试将地图添加到一个新位置,如下所示:
@GetMapping(value="/auth/stream/{param}", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public ResponseEntity<Flux<ServerSentEvent<LoginRequisition>>> userStream(@PathVariable String param) {
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type", "text/event-stream");
headers.add("Cache-Control", "no-cache");
headers.add("X-Accel-Buffering", "no");
return ResponseEntity.ok().headers(headers).body(myStreamManager.DoSomething(param));
}
location /app/ {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://127.0.0.1:8444/;
}
location /app/auth/stream/ {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://127.0.0.1:8444/;
proxy_http_version 1.1;
proxy_set_header Connection "";
}
location /app/ {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://127.0.0.1:8444/;
location /app/auth/stream {
proxy_http_version 1.1;
proxy_set_header Connection "";
}
}
但它不起作用。我还尝试使用子定位,如下所示:
@GetMapping(value="/auth/stream/{param}", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
public ResponseEntity<Flux<ServerSentEvent<LoginRequisition>>> userStream(@PathVariable String param) {
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type", "text/event-stream");
headers.add("Cache-Control", "no-cache");
headers.add("X-Accel-Buffering", "no");
return ResponseEntity.ok().headers(headers).body(myStreamManager.DoSomething(param));
}
location /app/ {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://127.0.0.1:8444/;
}
location /app/auth/stream/ {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://127.0.0.1:8444/;
proxy_http_version 1.1;
proxy_set_header Connection "";
}
location /app/ {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header Host $http_host;
proxy_pass http://127.0.0.1:8444/;
location /app/auth/stream {
proxy_http_version 1.1;
proxy_set_header Connection "";
}
}
但它也不起作用。Moerover,当我尝试这些方法时,即使我将最后两行添加到location/app/上,该方法仍然不起作用,但只要我删除新位置,它就会开始正常工作
有人知道发生了什么事吗