IP Cam returns 400: bad request when accessed through reverse proxy

I already have running a reverse proxy in nginx successfully. I have configured it to redirect everything to https and access different services behind it (jellyfin, squaremap plugin for minecraft, octoprint) so that I always have a secured connection and can use different services without specifying or opening different ports.

Now I am rather new to 3D printing and just recently bought a printer and implemented octoprint to control it remotely. Now I wanted to add an webcam so I can view the progress while I am not at home.

For this purpose I wanted to use a dbpower CAM0089 connected via LAN and also access it through the reverse proxy and ultimately integrate it into the octoprint web interface. However, if I try to connect to the cam through the reverse proxy, the cam responds with 400: bad request and I just can’t find out why. I read different threads for several days but could not find a problem which fits my situation or even a hint or tip that works for me.

Here is my current proxy configuration:

location /webcam/ {

            #proxy_pass http://192.168.178.12/videostream.cgi?rate=0&user=XXX&pwd=XXX;
            #proxy_set_header Connection $http_connection;
            #proxy_set_header Upgrade $http_upgrade;
            #proxy_set_header Connection "upgrade";
            #proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            #proxy_set_header Authorization "Basic $http_authorization";
            #proxy_set_header X-Scheme $scheme;
            #proxy_set_header Upgrade $http_upgrade;
            #proxy_set_header Connection $http_connection;
            
            proxy_pass http://192.168.178.12/; # webcam address
            proxy_set_header Host $http_host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_http_version 1.1;
            proxy_redirect off;
            proxy_set_header Authorization "Basic YWRtaW46MTIzNDU2";
        }

As you can see, I already tried a lot of options.

To try and find out what could cause the problem, I used tcpdump on my server to watch the traffic between nginx and the webcam and wireshark on my computer to watch the traffic between it and the webcam.

Here is the request from my computer:

GET / HTTP/1.1
Host: 192.168.178.12
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:135.0) Gecko/20100101 Firefox/135.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: de,en-US;q=0.7,en;q=0.3
Accept-Encoding: gzip, deflate
Authorization: Basic YWRtaW46MTIzNDU2
Connection: keep-alive
Upgrade-Insecure-Requests: 1
Priority: u=0, i

Here is the answer from the webcam:

HTTP/1.1 200 OK
Server: Netwave IP Camera
Date: Fri, 14 Feb 2025 20:26:35 GMT
Content-Type: text/html
Content-Length: 3169
Cache-Control: private
Connection: close

Here is the request from nginx:

GET / HTTP/1.1
Host: 192.168.178.12
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:135.0) Gecko/20100101 Firefox/135.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: de,en-US;q=0.7,en;q=0.3
Accept-Encoding: gzip, deflate, br, zstd
Authorization: Basic YWRtaW46MTIzNDU2
Connection: close
Upgrade-Insecure-Requests: 1
Priority: u=0, i
Cookie: xxx
Sec-Fetch-Dest: document
Sec-Fetch-Mode: navigate
Sec-Fetch-Site: none
Sec-Fetch-User: ?1

This is the answer from the webcam:

HTTP/1.1 400 Bad Request
Server: Netwave IP Camera
Date: Fri, 14 Feb 2025 20:53:16 GMT
Content-Type: text/html
Content-Length: 135
Connection: close

I rearranged the fields in the requests for better comparison, I hope the order is not important, otherwise I will provide the original order.

The only things that I could identify are the connection: close in the request over nginx rather than connection: keep-alive (but I already had a setting where this was also keep-alive over nginx, but still got bad request), and the additional Cookie und Sec-Fetch-*-Fields over nginx, but I am not sure if these could be the problem.

I am running out of ideas and was hoping to find answers that lead me in the right direction on this forum. If you need any more information please let me know and I will happily provide them.

Thank you in advance!

1 Like

Are you seeing anything in the nginx error logs or the camera logs? There definitely seems to be something that the camera does not like about having its traffic running through a proxy, but it is hard to tell exactly what the issue is without more information about the software that it is running.

I couldn’t see anything in the nginx logs which looked relevant, the cam has a very basic log that only shows logins and nothing more.

My approach to finding a fix was to build a little application in javascript to send custom requests to the ip cam. First, I added alle the fields from the direct request and then started adding the fields from the request sent by nginx. After I added the cookie field (which holds a lot of data), I got the bad request message from the cam.

My solution then was to add

proxy_set_heaader Cookie “”;

to the config and it finally worked.

I still don’t know why nginx adds the cookie field or why the cam didn’t like it, but I am happy that I got it working.

The only thing that catched my attention was that the http request had to be split up into two tcp packets because of the rather big cookie field exceeding the maximum packet size. So it could be that the tcp stack in the cam is not able to handle fragmented packets. But this is just a guess, my knowledge in this area is very limited.