[Solved] Pls help: 502 bad gateway - lighttpd response headers too large
lighttpd 1.4.63-1ubuntu3.1 running on Ubuntu 20.04. I'm a hobby user yet with decades of experience.
Very low-traffic, very low concurrence. CGI with python. Web pages work just fine for long, also with python.
Recently I implemented a private API for my personal usage on this server, with Python.requests. I'm using application/json for data transmission, only over http. It worked fine for days. Suddenly, today I'm getting "502 bad gateway" error, received by API as response. Lighty logs show "lighttpd response headers too large for xxx.py(the python script file I'm requesting)". This error occurs during every visit with identical error, while my site hosting remains normal.
I did software upgrade and system restart, no change.
Please help me to figure this out. TKS!
Here is the response I get:
<?xml version="1.0" encoding="iso-8859-1"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title>502 Bad Gateway</title> </head> <body> <h1>502 Bad Gateway</h1> </body> </html>
Replies (10)
RE: Pls help: 502 bad gateway - lighttpd response headers too large - Added by gstrauss 17 days ago
lighttpd src/http-header-glue.c
defines
/** * max size of the HTTP response header from backends * (differs from server.max-request-field-size for max request field size) */ #define MAX_HTTP_RESPONSE_FIELD_SIZE 65535
If you are abusing HTTP headers for your private API and are sending response header >= 64k, please consider alternatives to your abuse of HTTP response headers. The HTTP response body can be whatever you like.
RE: Pls help: 502 bad gateway - lighttpd response headers too large - Added by avij 17 days ago
I would guess the script does not output the Content-Type header properly. Your script should output something like this:
Content-Type: application/json { text: "Hello world" }
Try running the python script manually from the command line to see if it, for example, outputs some error which confuses lighttpd.
RE: Pls help: 502 bad gateway - lighttpd response headers too large - Added by gstrauss 17 days ago
I would guess the script is sending pure JSON and is neglecting to send any response headers before sending JSON.
A minimal response header before the JSON could be Status: 200\n\n
RE: Pls help: 502 bad gateway - lighttpd response headers too large - Added by letrois 17 days ago
gstrauss wrote in RE: Pls help: 502 bad gateway - lighttpd response headers...:
I would guess the script is sending pure JSON and is neglecting to send any response headers before sending JSON.
A minimal response header before the JSON could be
Status: 200\n\n
this seems did the trick. TKKS!
RE: Pls help: 502 bad gateway - lighttpd response headers too large - Added by letrois 17 days ago
avij wrote in RE: Pls help: 502 bad gateway - lighttpd response headers...:
I would guess the script does not output the Content-Type header properly. Your script should output something like this:
[...]
Try running the python script manually from the command line to see if it, for example, outputs some error which confuses lighttpd.
surely I did include this, otherwise it should not have worked for days. but I do not know why it worked why it worked when I did not print "Status: 200\n\n".
RE: Pls help: 502 bad gateway - lighttpd response headers too large - Added by letrois 17 days ago
gstrauss wrote in RE: Pls help: 502 bad gateway - lighttpd response headers...:
lighttpd
src/http-header-glue.c
defines
[...]If you are abusing HTTP headers for your private API and are sending response header >= 64k, please consider alternatives to your abuse of HTTP response headers. The HTTP response body can be whatever you like.
Sorry I forgot to mention that my requests are just several short strings.
RE: Pls help: 502 bad gateway - lighttpd response headers too large - Added by letrois 17 days ago
gstrauss wrote in RE: Pls help: 502 bad gateway - lighttpd response headers...:
I would guess the script is sending pure JSON and is neglecting to send any response headers before sending JSON.
A minimal response header before the JSON could be
Status: 200\n\n
I'm closing this thread. But, could you pls explain a bit further more, why it worked for days when I did not do this? TKS anyway bud
RE: Pls help: 502 bad gateway - lighttpd response headers too large - Added by letrois 17 days ago
Just noticed that, normally my response is of xxK. Today it reached xxxK, which would cost more time. Does this ring a bell?
RE: Pls help: 502 bad gateway - lighttpd response headers too large - Added by gstrauss 17 days ago
surely I did include this, otherwise it should not have worked for days. but I do not know why it worked why it worked when I did not print "Status: 200\n\n".
Please use a search engine to look up the definitions of a CGI request and response. RFC3875 from 2004 documents the practices from the 1990s. Responses have response headers followed by a blank line (LF or CRLF), followed by the (optional) response body. Some servers (including lighttpd) will parse HTTP-formatted response lines as CGI NPH (non-parsed headers), though lighttpd intentionally does parse them. FastCGI and other non-HTTP backends typically define a Status line, or might send a response with an HTTP-formatted response line, which lighttpd also supports similar to CGI NPH.
There are some examples in mod_fastcgi which demonstrate sending a Status line.
Just about every python framework has examples demonstrating sending a Status line.
The reason lighttpd worked with your invalid python responses for smaller responses is that there are more than a few people like yourself who write code without first looking at the basic specifications for with what your code is interfacing. If lighttpd receives a small response from a CGI backend and does not detect proper format for response headers, then lighttpd will treat the response as Status: 200
and send the entire response as the response body. If the backend is something else, like FastCGI, and the first line of the response does not look like a header (i.e. contains a :
), and the response is <= 2 lines, then lighttpd tries to treat the response as the response body. These are heuristics. You should prefer to always provide a Status
line in your FastCGI responses.
RE: [Solved] Pls help: 502 bad gateway - lighttpd response headers too large - Added by letrois 16 days ago
I should have done more homework. Thanks for the perfect explanation.