<p>I have write a server side sse test api like this:</p>
<pre><code>public SseEmitter testStream(String question) {
        SseEmitter emitter = new SseEmitter();

        // Start asynchronous processing
        new Thread(() -&gt; {
            try {
                for (int i = 0; i &lt; 10; i++) {
                    // Generate some event data
                    String eventData = &quot;Event data &quot; + i;

                    // Create Server-Sent Event object
                    ServerSentEvent event = ServerSentEvent.builder()
                            .event(&quot;message&quot;)
                            .data(eventData)
                            .build();

                    // Serialize event to string and send to client
                    String serializedEvent = JSON.toJSONString(event);
                    emitter.send(serializedEvent);

                    // Wait for one second before sending the next event
                    Thread.sleep(1000);
                }

                // Complete the SSE stream
                emitter.complete();
            } catch (Exception e) {
                emitter.completeWithError(e);
            }
        }).start();

        return emitter;
    }
</code></pre>
<p>then expose the api as a rest api:</p>
<pre><code>@GetMapping(path=&quot;/test&quot;,produces = MediaType.TEXT_EVENT_STREAM_VALUE)
SseEmitter testStream(@RequestParam(required = true) String question);
</code></pre>
<p>when I invoke this api in local machine, the sse message returned one by one every seconds, that's what I want. But after I deploy this api to remote cloud server, the api wait a long time and return the result onece. Am I missing something? what should I do to make the remote server return the message one by one not onece and waiting for a long time. This is my test command:</p>
<pre><code># localhost test 
curl -X GET -H 'Content-Type: application/json' -N http://localhost:11014/ai/stream/chat/test\?question\=1
# remote server test
curl -X GET -H 'Content-Type: application/json' -H 'x-request-id:1'  -H 'x-access-token: eyJhbGciOiJIUzUx.balabala.sDM53g' -N https://ai.example.top/ai/stream/chat/test\?question\=1
</code></pre>
<p>the route to remote service like this:</p>
<p>client user&gt;dns&gt;nginx&gt;kubernetes traefik&gt;spring gateway in kubernetes&gt;kubernetes service&gt;kubernetes pod</p>
<p>I have tried to add cache control for request header in curl command like this <code>-H 'Cache-Control:no-cache'</code> and response header:</p>
<pre><code>response.setContentType(&quot;text/event-stream&quot;);
response.setCharacterEncoding(&quot;UTF-8&quot;);
response.setHeader(&quot;Cache-Control&quot;, &quot;no-cache&quot;);
response.setHeader(&quot;Connection&quot;, &quot;keep-alive&quot;);
</code></pre>
<p>seems did not work. I also tried to let nginx no cache the content like this:</p>
<pre><code>server {
        listen       443 ssl;
        server_name  ai.example.top;
        
        ssl_certificate /etc/letsencrypt/live/poemhub/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/poemhub/privkey.pem;

        if ($allowed = 0) {
            return 403;
        }
        
        location / {
            client_max_body_size 1m;
            proxy_redirect off;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_read_timeout 3600;
            proxy_pass http://127.0.0.1:8000;
        }

        location ^~ /post/websocket {
                    proxy_redirect off;
                    proxy_set_header Host $host;
                    proxy_set_header X-Real-IP $remote_addr;
                    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                    proxy_pass http://127.0.0.1:8000;
                    proxy_http_version 1.1;
                    proxy_set_header Upgrade $http_upgrade;
                    proxy_set_header Connection &quot;upgrade&quot;;
                }

        location ^~ /ai/stream {
            proxy_redirect off;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://127.0.0.1:8000;
            proxy_cache_bypass $http_pragma;
        }
}
</code></pre>

Dolphin
<p>Finally I found the nginx cached the sse message, configure the nginx like this to fixed this issue:</p>
<pre><code>location ^~ /ai/stream {
            proxy_redirect off;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_pass http://127.0.0.1:8000;
            proxy_cache_bypass $http_pragma;
            proxy_http_version 1.1;
            proxy_set_header Connection '';
            proxy_buffering off;
            proxy_cache off;
            gzip off;
            chunked_transfer_encoding off;
        }
</code></pre>
