'Nginx causes file upload to freeze

I've been trying to figure this out for days now.

When I attempt to upload a file to my webserver written in java, about 2.5MB of the file uploads and then it just freezes. Nginx appears to be the culprit because when I upload the file to the webserver directly to the port 1234 using my vps's direct ip instead of the domain the full file uploads perfectly fine.

I am using a program also written in java to upload the file to the webserver and I am getting the error on that:

Exception in thread "main" java.io.IOException: Premature EOF
        at sun.net.www.http.ChunkedInputStream.readAheadBlocking(ChunkedInputStream.java:565)
        at sun.net.www.http.ChunkedInputStream.readAhead(ChunkedInputStream.java:609)
        at sun.net.www.http.ChunkedInputStream.read(ChunkedInputStream.java:696)
        at java.io.FilterInputStream.read(FilterInputStream.java:133)
        at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.read(HttpURLConnection.java:3456)
        at sun.net.www.protocol.http.HttpURLConnection$HttpInputStream.read(HttpURLConnection.java:3449)
        at java.nio.file.Files.copy(Files.java:2908)
        at java.nio.file.Files.copy(Files.java:3027)
        at me.hellin.Main.uploadFile(Main.java:28)
        at me.hellin.Main.main(Main.java:23)

This is my nginx config for it:

server {
    listen 80;
    server_name *redacted*;
    proxy_read_timeout 300;
    proxy_connect_timeout 300;
    proxy_send_timeout 300;

    location / {
       client_max_body_size 100M;
       proxy_pass http://localhost:1234;
    }
}

server {
    client_max_body_size 100M;
    client_body_timeout 120s;
    client_body_temp_path /tmp;
}

This is what I see in nginx error.log:

2022/05/03 14:14:41 [error] 2085134#2085134: *326930 connect() to [::1]:1234 failed (101: Network is unreachable) while connecting to upstream, client: *redacted*, server: *redacted*, request: "POST / HTTP/1.1", upstream: "http://[::1]:1234/", host: "*redacted*"

Here's my code just in case I did something wrong here that somehow only affects nginx:

private static InputStream upload(File file) throws Exception {
        HttpURLConnection httpURLConnection = (HttpURLConnection) new URL("*redacted*")
                .openConnection();

        httpURLConnection.setDoOutput(true);

        httpURLConnection.setRequestProperty("content-length", String.valueOf(file.length()));
        httpURLConnection.setRequestProperty("content-type", "application/java-archive");

        httpURLConnection.setRequestMethod("POST");

        OutputStream outputStream = httpURLConnection.getOutputStream();
        Files.copy(file.toPath(), outputStream);

        outputStream.close();

        return httpURLConnection.getInputStream();
    }


Solution 1:[1]

I have finally found the solution to the infuriating issue. Turns out that nginx does some weird shit and I had to change the servers code (receiving the file) to send its response only after the server had closed the output stream. I was sending a response back to the client before and Ig nginx saw that and closed the connection.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Hellin