0

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();
    }
Hellin
  • 1
  • 3
  • 1
    "*When I upload it directly (not through nginx)*" - what does this mean? Are you talking about another protocol, FTP or SCP or something? How are you uploading "*with nginx*"? Are you using PHP or some other server-side scripting language? If yes, have you checked that language's upload-related config options? Eg for PHP: https://stackoverflow.com/questions/2184513/change-the-maximum-upload-file-size What do your nginx and scripting language logs show? What does your browser devtools show, if you click the network tab and inspect the request? – Don't Panic May 02 '22 at 07:39
  • Basically I have a http webserver written in java listening on a specific port which I can upload files to. A nginx reverse proxy is set up on a specific domain listening on port 80 but what I did was I went to tried upload it to the vps' ip and port directly instead of using the domain which allowed the full file upload. – Hellin May 02 '22 at 14:18
  • Also getting this error on the client side: 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) – Hellin May 02 '22 at 14:19
  • "*upload it to the vps' ip and port directly*" - that's still nginx though ... ? Or do you mean upload directly to your Java app (on `1234`?)? In any case, it sounds like there is quite a lot of pretty relevant info missing from your question, I'd maybe suggest editing it and trying to explain the problem again. – Don't Panic May 03 '22 at 07:18
  • just posted the edits. – Hellin May 03 '22 at 18:21

1 Answers1

0

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.

Hellin
  • 1
  • 3
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 12 '22 at 17:08